javajunkies.org (2382) Re: Java 2 A Beginners Guide:: A community committed to sharing Java knowledge and coding tips. claiming to be aimed soley at beginners should definately make it easy to http://www.javajunkies.org/index.pl?node_id=2382&lastnode_id=41HOME | 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.
Hitmill.com - Java for Students:: Java for Students is a beginners page for students new to Java programming. Java Standard Edition Platform Documentation (Sun Microsystems) http://www.hitmill.com/programming/java/beginners.htmlHOME | No title:: Professor Matloffs Java Beginners Web Page His Java tutorial. Dr. Matloffs beginners guide to installing and using Linux. http://heather.cs.ucdavis.edu/~matloff/java.htmlHOME | 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.
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(Stringargs){
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);
}
} Coding Patterns:: Coding Patterns for Java Beginners. Version 7. Joseph Bergin. Pace University. jbergin@pace.edu Not everything here is Java specific, however. http://csis.pace.edu/~bergin/patterns/codingpatterns.htmlHOME |
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!
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> Java > Beginners Lab Assignments:: Programming Resources & Code Examples for visual basic, java, php, asp, c, c++, assembly Calculator In Java. Calculator with both Standard and Scientific Mode http://www.java.happycodings.com/Beginners_Lab_Assignments/index.htmlHOME | Beginners Guides : BetterWorld.com:: Buy Beginners Guides Used Books, New Beginners Guides Books Home / Computers & Internet / Programming / Java / Beginners Guides. Browse Our Books http://www.betterworld.com/category-C3612.aspxHOME | =0? (""+gpa):
"UNKNOWN";
return basic+". GPA: "+gpastring;
}
}
----------AnotherClass.java: cut here---------
public class AnotherClass{
public static void main(Stringargs){
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);
}
}
How much does getting a small tattoo on your hip/stomach hurt?
Do anyone else have an itchy anus? ?
|