Create a class called student, this class contains: •Three private instance vari
ID: 3684237 • Letter: C
Question
Create a class called student, this class contains:
•Three private instance variables: age(date type: int) ,name (date type:
String) and Major(date type: String);
•Two overloaded constructors: the first constructor doesn't have
parameters , it sets both age, name and Major to default (age: 20, name:
Tom, Major: IT); The second constructor has three parameters, these
three parameters can initialize both age, name and Major as specified.
•Three public methods:
getName():A public method for retrieving student's name;
setAge(int studentage):A public method that sets the age of the
student.
getCollegeName(): return student's college name( return Strome if
student's major is IT , Marketing , Finance or Management, return
Sciences if student's major is CS or Physics, return others if student's
major is not on the list above)
•The class should contain a main() method which tests two overloaded
constructors and three public methods: getName(), setAge(int
studentage)and getCollegeName()
Explanation / Answer
class student
{
int age;
String name, Major;
student()
{
age=20;
name="Tom";
Major="IT";
}
student(int a, String n, String m)
{
age=a;
name=n;
Major=m;
}
public String getName()
{
return name;
}
void setAge(int studentage)
{
age=studentage;
}
public String getCollegeName()
{
if(Major.equals("IT") || Major.equals("Marketing") || Major.equals("Finance") || Major.equals("Management"))
return "Strome";
else if(Major.equals("CS") || Major.equals("Physics"))
return "Sciences";
else
return "Others";
}
public static void main(String args[])
{
student o=new student();
student o1=new student(18,"Bob","IT");
String s1=o1.getName();
o1.setAge(19);
String s2=o1.getCollegeName();
System.out.println("Student name ="+s1);
System.out.println("College name ="+s2);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.