Google Answers Logo
View Question
 
Q: java (beginner) ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: java (beginner)
Category: Computers > Programming
Asked by: brittlehorse-ga
List Price: $10.00
Posted: 03 Nov 2002 20:48 PST
Expires: 03 Dec 2002 20:48 PST
Question ID: 97828
I realize this may be easy for a someone, but hey, i don't have the
time.

I have a hypothetical class called Student.  
A) I need to write two (2) constructors for that class that allow the
initialization of the class variables.  One constructor will
initialize only Name and Age, while the other will initialize name,
grade point average, and age.

B) Assume you are in a different class and you need to create an
object named disobedient Student with an initial name (you decide
which name), grade point average of 1.5, and age = 27.  Write the code
like a beginner to create this object from the second constructor
method created above.
Answer  
Subject: Re: java (beginner)
Answered By: rbnn-ga on 03 Nov 2002 21:16 PST
Rated:5 out of 5 stars
 
Hi,

I'm not sure what your level of expertise exactly is, so I think it's
easiest if I just provide the code, and if you have any questions for
me, just use the "Request Clarification" button (before rating the
answer). I will be happy to help.

By the way, a nice site to learn java is http://www.topcoder.com . It
has programming competitions, they force you to get pretty fast, but
they are kind of fun.

Anyway, You need two classes, one for the student, which I call
Student, and which MUST be in a file named Student.java, and one for
the other class, which I call "AnotherClass.java" .

Once you put them in the files, you can compile and run the code as
usual.

I hope the code is fairly straightforward. The only think I did that
was a little tricky is that I felt that some students might not even
have a GPA yet, like incoming freshman, so I set their GPA to "-1" .

Then in the "toString()" method of the Student class I check if the
GPA is <0, and if it is I print "UNKNOWN" for the GPA .

First, I will give a script to show what the code looks like when run:

# compile the code...
% javac AnotherClass.java 

# run the code
% java AnotherClass
The good student...Name: george. Age: 56. GPA: UNKNOWN

The disobedient student...Name: brittlehorse-ga. Age: 27. GPA: 1.5



----------Student.java [cut here]-------------
public class Student{
    public String name;
    public int age;
    public double gpa=-1; //for unknown GPA

    public Student(String name, int age){
	this.name=name;
	this.age=age;
    }

    public Student(String name, int age,double gpa){
	this.name=name;
	this.gpa=gpa;
	this.age=age;
    }

    public String toString(){
	String basic="Name: "+name+". Age: "+age;
	String gpastring= 
	    gpa>=0? (""+gpa):
	    "UNKNOWN";
	return basic+". GPA: "+gpastring;
    }
}
    
----------AnotherClass.java: cut here---------
public class AnotherClass{
    public static void main(String[]args){
	Student disobedientStudent=new Student("brittlehorse-ga",27,1.5);
	Student goodStudent=new Student("george",56);
	System.out.println("The good student..."+goodStudent);
	System.out.println("The disobedient student..."+disobedientStudent);
    }
}

Request for Answer Clarification by brittlehorse-ga on 03 Nov 2002 21:32 PST
I am unfamiliar with the shortcut stuff like " (""+gpa): " and all the
pluses "++".  Could you dumb it up a bit for this beginner?
 
also, could you modify the "this" part?  I can't use it.
I certainly appreciate your help with this and plan on giving you
great feedback.

Clarification of Answer by rbnn-ga on 03 Nov 2002 22:09 PST
Hi,

Sure, I'd be happy to simplify it!

I made the field names (what you called class variables) slightly
different from the arguments passed in, so I got rid of the the "this"
statements.

And I moved the printing work to AnotherClass so that now there is no
more + stuff anywhere.

I hope this is helpful and, as always, please feel free to ask for
additional clarification if you have any questions.

Oh, make sure to delete the old versions of the files! Otherwise
things can get confused.

Sample run:

% javac AnotherClass.java
java AnotherClass
The name of disobedient student is: brittlehorse-ga

The age of disobedientStudent is: 27

The GPA of disobedientStudent is: 1.5



--------------Cut here for Student.java------
public class Student{
    public String Name;
    public int Age;
    public double GPA=-1; //for unknown GPA

    public Student(String name, int age){
	Name=name;
	Age=age;
    }

    public Student(String name, int age,double gpa){
	Name=name;
	GPA=gpa;
	Age=age;
    }
}
    
-----------Cut here for AnotherClass.java-----------------

public class AnotherClass{
    public static void main(String[]args){
	Student disobedientStudent=new Student("brittlehorse-ga",27,1.5);
	Student goodStudent=new Student("george",56);
	System.out.print("The name of disobedient student is: ");
	System.out.println(disobedientStudent.Name);
	
	System.out.print("The age of disobedientStudent is: ");
	System.out.println(disobedientStudent.Age);
	
	System.out.print("The GPA of disobedientStudent is: ");
	System.out.println(disobedientStudent.GPA);
    }
}
brittlehorse-ga rated this answer:5 out of 5 stars
great helper!  Very easy to work with and is a very good communicator.
 The site recommended was too advanced for this beginner, but still-
THANKS A BUNCH!!!  I Value Your Answer!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy