where a student is a person. A person has a first name (string), a last name (st
ID: 3635702 • Letter: W
Question
where a student is a person. A person has a first name (string), a last name (string), and age (int). A student has a major (string), a department (string), and a year admitted (int). Each class should include the following methods:A default constructor
One or more special constructor(s) that accepts initialization parameters
Accessors and mutators for the class’ data attributes
A toString() method that returns a string describing a class instance
A main() method that tests the class by creating three instances of the class, initializing them using either the special constructor(s) or the mutators (show at least one case of each), then print the instances’ information
Explanation / Answer
Please Rate:Thanks
import java.util.Scanner;
class Person
{
public String fname;
public String lname;
public int age;
Scanner keyboard = new Scanner(System.in);
public Person()
{
}
public Person(String f,String l,int a)
{
fname=f;
age=a;
lname=l;
}
public String toString()
{
return (fname + " " + lname+" "+age);
}
public void setfName(String first)
{
fname = first;
}
public void setage(int nage)
{
age=nage;
}
public void setlName(String last)
{
lname = last;
}
public void setPerson(String f,String l,int a)
{
fname=f;
age=a;
lname=l;
}
public void read()
{
System.out.println("Please enter first name");
fname = keyboard.nextLine();
System.out.println("Please enter last name");
lname = keyboard.nextLine();
System.out.println("Please enter age");
age = keyboard.nextInt();
}
public String getfName()
{
return fname;
}
public int getage()
{
return age;
}
public String getlName()
{
return lname;
}
public boolean hasSameName(Person n)
{
return fname==n.fname;
}
}
-------------------------------------------------
public class Student extends Person {
public String major;
public String department;
public int year;
public Student()
{
}
public Student(String fname, String lname, int age, String nmajor, String ndepartment,int nyear){
super(fname,lname,age);
major=nmajor;
department=ndepartment;
year=nyear;
}
public String toString()
{
return (fname + " " + lname+" "+age+" "+major+" "+department+" "+year);
}
public void setmajor(String nmajor)
{
major=nmajor;
}
public void setdepartment(String ndep)
{
department=ndep;
}
public void setyear(int nyear)
{
year=nyear;
}
public void read()
{
System.out.println("Please enter first name");
fname = keyboard.nextLine();
System.out.println("Please enter last name");
lname = keyboard.nextLine();
System.out.println("Please enter age");
age = keyboard.nextInt();
System.out.println("Please enter major");
major = keyboard.nextLine();
System.out.println("Please enter department");
department = keyboard.nextLine();
System.out.println("Please enter year");
year = keyboard.nextInt();
}
public String getmajor()
{
return major;
}
public String getdep()
{
return department;
}
public int getyear()
{
return year;
}
}
---------------------------------------------
public class TestDemo {
public static void main(String[] args){
Person p1=new Person("Ram","Kumar",29);
Person p2=new Person();
p2.read();
Student s1=new Student("pavan","kumar",20,"major","computers",2010);
Student s2=new Student();
s2.read();
System.out.println(p1.toString());
System.out.println(p2.toString());
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.