Write a Student Class that encapsulates a Student. The instance variables will b
ID: 3539485 • Letter: W
Question
Write a Student Class that encapsulates a Student. The instance variables will be a string that will hold the name an integer that will hold the age and a boolean fulltime that is set to true or false. Student will have following methods: read(), setName(String), setAge(int), setFulltime(boolean), set Student (String, int, boolean), String getName(), int getAge(), boolean getFulltime() boolean equals(Student), boolean hasSameName(Student), boolean hasSameAge(Student), boolean isOlderThan(Student), toString(). Write a main that tests all of the methods you have written.
Explanation / Answer
import java.util.Scanner;
public class Student {
String name;
int age;
boolean fulltime;
Student(){
read();
}
void read(){
System.out.println("Enter the name, age & value for fulltime");
Scanner nv = new Scanner(System.in);
name = nv.nextLine();
age = nv.nextInt();
fulltime = nv.nextBoolean();
setStudent(name, age, fulltime);
System.out.println(toString());
}
void setName(String nam){
name = getName();
}
void setAge(int ag){
age = getAge();
}
void setFulltime(boolean full){
fulltime = full;
}
void setStudent(String nam, int ag, boolean full){
setName(nam);
setAge(ag);
setFulltime(full);
}
String getName(){
return name;
}
int getAge(){
return age;
}
boolean getFulltime(){
return fulltime;
}
boolean hasSameName(Student stu){
if(name == stu.name) return true;
else return false;
}
boolean hasSameAge(Student stu){
if(age == stu.age) return true;
else return false;
}
boolean isOlderThan(Student stu){
if(age < stu.age) return true;
else return false;
}
public String toString(){
return String.format("name = %s, age = %d", name, age);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.