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);
}
} |
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);
}
}
|