11 Replies - 778 Views - Last Post: 25 January 2009 - 03:03 PM Rate Topic: -----

#1 needc++help  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 16-March 08

Class Object

Posted 23 January 2009 - 04:07 PM

Hey Guys,

I'm lost on where to start on this one. I have the following code
import java.util.Scanner;
public class Age {
	/**
	 * @param args
	 */
public static void main(String[] args) {
	int birthYear;
	int currentYear; 
	int age;
	Scanner inputObject = new Scanner(System.in);

	System.out.print("Enter the year of your birth ");
	birthYear = inputObject.nextInt();

	System.out.print("Enter the year for which you wish to know your age ");
	currentYear = inputObject.nextInt(); 
	age = currentYear - birthYear;
	
	System.out.println("You are " + age + " Years Old"); 
	System.out.println("You are " + age*12 + " Months Old"); 
	System.out.println("You are " + age*365 + " Days Old");
}
}

I need to take that code and rewrite it to use an age class object to handle to calculation of the age and displaying of the results. Obviously, I understand that these parts
birthYear = inputObject.nextInt();

currentYear = inputObject.nextInt(); 
age = currentYear - birthYear;

System.out.println("You are " + age + " Years Old"); 
System.out.println("You are " + age*12 + " Months Old"); 
System.out.println("You are " + age*365 + " Days Old");
need to be included in a different class. I'm not looking for an answer, but maybe just some advice on what is expected since I am not even completely sure what is even meant by a class object. Could someone clarify this for me?

Thanks in advance!!

Is This A Good Question/Topic? 0
  • +

Replies To: Class Object

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Class Object

Posted 23 January 2009 - 04:18 PM

Actually, to get the days right you'd have to consider leap years..

I wrote a snippet that you might have found useful..

Calendar for Java

This post has been edited by Gloin: 23 January 2009 - 04:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 needc++help  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 16-March 08

Re: Class Object

Posted 23 January 2009 - 05:00 PM

View PostGloin, on 23 Jan, 2009 - 03:18 PM, said:

Actually, to get the days right you'd have to consider leap years..

I wrote a snippet that you might have found useful..

Calendar for Java

I appreciate the snippet, but I'm still left confused. I really don't know what's expected. I've tried looking around to see what is meant by a class object. I know it's can't be anything too complex.
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Class Object

Posted 23 January 2009 - 05:14 PM

So you want to know about class Objects, purist wont like you calling it that though, really its a class instance. At the moment you are doing everything in your main. Rather than that, you can create a class with no main which holds all constructors and methods. In your case, u could have an class called age, which has a constructor which takes birthyear, currentyear.
public class Age
{
int birthYear;
int currentYear;

public Age(int birthYear, int currentYear)
{
this.birthYear=birthYear;
this.currentYear=currentYear;
}

}


Now the constructor in the class gives us access to an Age instance. Then in another class, you can create an instance of this Object, like so
public class test
{
public static vois main(String[] args)
{
	System.out.print("Enter the year of your birth ");
	int birthYear = inputObject.nextInt();

	System.out.print("Enter the year for which you wish to know your age ");
	int currentYear = inputObject.nextInt(); 

	Year personsYear = new Year(birthYear, currentYear)// creating the instance of Age class by passing the constructor the 2 required variables.
}
}


So that shows how to create an instance(Object) of a class. Now in your age class, you can add methods. One for you could be like
public int getAge()
{
int age = currentYear - birthYear;
return age;
}


Then in the tester class, u can you the instance of the class u made to access this method.
int age = personsYear.getAge();


This just shows a basic concept as to what creating class instances does.

This post has been edited by nick2price: 23 January 2009 - 05:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 needc++help  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 16-March 08

Re: Class Object

Posted 24 January 2009 - 08:42 PM

Ok....with some work, I think I'm making progress. Right now my code looks like
import java.util.Scanner;
public class Age 
{
	int birthYear;
	int currentYear;
	
public Age(int birthYear, int currentYear)
	{
	this.birthYear=birthYear;
	this.currentYear=currentYear;
	}
public static void main(String[] args) 
	{
	Scanner inputObject = new Scanner(System.in);

	System.out.print("Enter the year of your birth ");
	int birthYear = inputObject.nextInt();

	System.out.print("Enter the year for which you wish to know your age ");
	int currentYear = inputObject.nextInt(); 

	Year personsYear = new Year(birthYear, currentYear);
	}
public int getAge()
	{
	int age = currentYear - birthYear;
	return age;
	}
}


I'm running into problems on the Year personsYear=new Year(birthYear, currentYear); line. Year cannot be resolved to a type. I'm sure there will be more problems after this one, but atleast I can continue rolling after some help.

Thanks in advance!
Was This Post Helpful? 0
  • +
  • -

#6 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,101
  • Joined: 26-September 08

Re: Class Object

Posted 24 January 2009 - 11:18 PM

QUESTIONS

1: What is Year. Where did that come from?

2: Is this age object supposed to contain a main method? When dealing with objects it is more typical to treat the constructor as you were treating the Main method, and shoving anything you want the object to do when it is instantiated in there.

3: How many total .class files are you supposed to have?
Was This Post Helpful? 0
  • +
  • -

#7 needc++help  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 16-March 08

Re: Class Object

Posted 25 January 2009 - 08:53 AM

View PostTriggaMike, on 24 Jan, 2009 - 10:18 PM, said:

QUESTIONS

1: What is Year. Where did that come from?

2: Is this age object supposed to contain a main method? When dealing with objects it is more typical to treat the constructor as you were treating the Main method, and shoving anything you want the object to do when it is instantiated in there.

3: How many total .class files are you supposed to have?


I'm not sure what Year is supposed to be. That is just what I kind of put together from the previous post that I got help on. The only guidelines I was given was to just have an age class instance handle the calculations and the display of the results (see my first post). Sorry if this post has become unclear, but I'm really not sure what I am doing and keep changing my code as I go.
Was This Post Helpful? 0
  • +
  • -

#8 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,101
  • Joined: 26-September 08

Re: Class Object

Posted 25 January 2009 - 11:40 AM

Ok then you should not have a public static void main(String[] args) in that object. You will need to make a second class file in the same area and you will need to do something like Age age = new Age();
Was This Post Helpful? 0
  • +
  • -

#9 needc++help  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 16-March 08

Re: Class Object

Posted 25 January 2009 - 11:51 AM

I've made changes that atleast allow me to compile the code. The problem is that there is a problem with the calculations because when it displays the age it always displays 0. Here is a look at my code
import java.util.Scanner;
public class Age 
{
	int birthYear;
	int currentYear;
	static int age;
	
public Age(int birthYear, int currentYear)
	{
	this.birthYear=birthYear;
	this.currentYear=currentYear;
	}
public static void main(String[] args) 
	{
	Scanner inputObject = new Scanner(System.in);

	System.out.print("Enter the year of your birth ");
	int birthYear = inputObject.nextInt();

	System.out.print("Enter the year for which you wish to know your age ");
	int currentYear = inputObject.nextInt(); 
	
	System.out.println("You are " + age + " Years Old");
		}
public int getAge()
	{
	int age = currentYear - birthYear;
	return age;
		}
}


Was This Post Helpful? 0
  • +
  • -

#10 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Class Object

Posted 25 January 2009 - 12:12 PM

Because you read in the ints, assign them to local variables, then never use them, instead calling the age variable of the class, which, I assume, you changed to static to get the class to compile. Do not make it static, instead create an object of this class with the read in values, then call the getAge method
Was This Post Helpful? 0
  • +
  • -

#11 needc++help  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 16-March 08

Re: Class Object

Posted 25 January 2009 - 01:36 PM

Is something like this getting closer? It doesnt like System.out.println where I have it though...
import java.util.Scanner;
public class Age 
{
	int birthYear;
	int currentYear;
		
public static void main(String[] args) 
	{
	Scanner inputObject = new Scanner(System.in);

	System.out.print("Enter the year of your birth ");
	int birthYear = inputObject.nextInt();

	System.out.print("Enter the year for which you wish to know your age ");
	int currentYear = inputObject.nextInt(); 
	
	}
public int getAge()
	{
	int age = currentYear - birthYear;
	return age;
	}
Age personsAge = new age(birthYear, CurrentYear)
	{
	System.out.println("You are " + age + " Years Old");
	}
}

Was This Post Helpful? 0
  • +
  • -

#12 nexes  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 25-January 09

Re: Class Object

Posted 25 January 2009 - 03:03 PM

If I understand correctly, you need two classes, one class to do the age calculation and the other class as the "main" class. This may help you...

Age class is as follows
public class Age {

	private  int birthYear;
	private  int currentYear;

   public int calculateAge(int ageY, int curY){

	   int theAge;
	   birthYear = ageY;
	   currentYear = curY;

	   theAge = currentYear - birthYear;

	   return theAge;
   }

}



this class does nothing but take two variables that you send it and preform the math

this is the main class

import java.util.Scanner;

public class AppController {

	public static void main (String[] args){

		int x, y;
		int answer;
		Age age = new Age();  // here is where you set up an instance of the Age class

		Scanner scn = new Scanner(System.in);

		System.out.print("Enter the year you were born: ");
		x = scn.nextInt();
		System.out.print("Enter the current year: ");
		y = scn.nextInt();
		
		answer = age.calculateAge(x, y);

		System.out.println("you are " +answer+ " years old");
		System.out.println("in months " +answer*12+ " old");
		System.out.println("in days " +answer*365+ " old");

	}

}



I hope this helps. Keep in mind, that i was able to compile and run this independent of actually extending my appcontroller class to be a subclass of Age.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1