JAVA homework help thank you! In the next questions, write a complete class defi
ID: 3680739 • Letter: J
Question
JAVA homework help thank you!
In the next questions, write a complete class definition for a class called School. Use good principles of class design and encapsulation.
A school is described by its name.
Question 1
Write the class header and the instance data variable for the School class.
Question 2
Write two constructors:
a default (no-argument) constructor sets the name of the school to the text "UNKNOWN"
a second constructor sets the name based on a parameter
Question 3
Write appropriate getter/setter methods.
Question 4
Write a toString method that outputs "Name: " followed by the name of the school.
Explanation / Answer
Hi, Please find below School class implemented as per requirement.
School.java
public class School {
private String name;
private String address;
private String phone;
private int fee;
public int getFee() {
return fee;
}
public void setFee(int fee) {
this.fee = fee;
}
public School(){
name = "UNKNOWN";
}
public School(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString(){
System.out.println("Name : "+name);
return "";
}
}
Main.java
public class Main {
public static void main(String[] args) {
School s1 = new School();
System.out.println(s1.toString());
School s2 = new School("LOTUS SCHOOl");
System.out.println(s2.toString());
}
}
Output:
Name : UNKNOWN
Name : LOTUS SCHOOl
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.