package javaapplication1;
public class Main
{
public static void main(String[] args)
{
Student stu = new Student();
System.out.println (stu.getStudentid() +":" + stu.getStudentname()+":"+stu.getStudentgrade());
//1:Bill:A
Student stu2 = new Student ();
System.out.println (stu2.getStudentid() +":" + stu2.getStudentname()+":"+stu2.getStudentgrade());
//2:Mary:B
Student stu3 = new Student ();
System.out.println (stu3.getStudentid() +":" + stu3.getStudentname()+":"+stu3.getStudentgrade());
//3:Jame:C
Student stu4 = new Student ();
System.out.println (stu4.getStudentid() +":" + stu4.getStudentname()+":"+stu4.getStudentgrade());
//4:Helen:D
}
}
class Student
{
int Studentid;
String Studentname;
String Studentgrade;
Student()
{
Studentid = 1;
Studentname = "Bill";
Studentgrade = "A";
}
int getStudentid()
{
return Studentid;
}
String getStudentname()
{
return Studentname;
}
String getStudentgrade()
{
return Studentgrade;
}
}
Problem 1: Design and implement a Student class which has - Data members: studendID, studentName, and studentGrade - Constructor: Student (int id, String name) which will set studentID to the argument ‘id’ and studentName to the argument ‘name’ (studentID = id; studentName = name) - Method members: setGrade( ), getGrade ( ), getName( ), and getID( )
Problem 2: Implement a Test class which has a ‘main’ function. The ‘main’ function will create 4 student objects from the Student class above with ids and names: 1, Bill; 2, Mary; 3, Jame; 4, Helen and set their grades to A, B, C, D. The main ( ) function will output: ID:Name:Grade 1:Bill:A 2:Mary:B 3:Jame:C 4:Helen:D This is an example that he gave us.
class Cis36L0413
{
public static void main( String[] args )
{
NotSimple nsObj1 = new NotSimple();
System.out.println( "nsObj1.data is\t" + nsObj1.getData() );
System.out.println( "nsObj1.str is \t" + nsObj1.getStr() );
NotSimple nsObj2 = new NotSimple( 50,
"Another immutable string!" );
System.out.println( "nsObj2.data is\t" + nsObj2.getData() );
System.out.println( "nsObj2.str is \t" + nsObj2.getStr() );
nsObj2 = nsObj1;
System.out.println( "nsObj2.data is\t" + nsObj2.getData() );
System.out.println( "nsObj2.str is \t" + nsObj2.getStr() );
return;
}
}
class NotSimple
{
NotSimple()
{
data = 5;
str = new String( "Initialized!" );
}
NotSimple( int i, String str1 )
{
data = 5;
str = str1;
}
void setData( int i )
{
data = i;
return;
}
int getData()
{
return data;
}
void setStr( String str1)
{
str = str1;
return;
}
String getStr()
{
return str;
}
private int data;
private String str;
}
1) How do I change the data so I can print out 2:Mary:B on the second line? and 3:Jame:C on the third and 4:Helen:D on the fourth? So far I am printing out 4lines of 1:Bill:A. 2) If I am doing this wrong, can you please give me some examples?
thank you in advance
Hi, I am just following up on this. This is the correct coding that he wanted.
package test;
public class Test {
public static void main(String[] args) {
Student stu1 = new Student(1,"Bill");
stu1.setGrade ("A");
System.out.println(stu1.getId()+":"+stu1.getName()+":"+ stu1.getGrade());
Student stu2 = new Student (2,"Mary");
stu2.setGrade ("B");
System.out.println(stu2.getId()+":"+stu2.getName()+":"+ stu2.getGrade());
Student stu3 = new Student (3,"Jame");
stu3.setGrade ("C");
System.out.println(stu3.getId()+":"+stu3.getName()+":"+ stu3.getGrade());
Student stu4 = new Student (4,"Helen");
stu4.setGrade ("D");
System.out.println(stu4.getId()+":"+stu4.getName()+":"+ stu4.getGrade());
}
}
class Student{
int studentId;
String studentName;
String studentGrade;
Student(int id, String name){
studentId = id;
studentName = name;
}
public String getName(){
return studentName;
}
public int getId(){
return studentId;
}
void setGrade(String i)
{
studentGrade =i;
return;
}
public String getGrade(){
return studentGrade;
}
}