Please help in Java using Inheritence: Write a TitledPerson class, which you der
ID: 3806954 • Letter: P
Question
Please help in Java using Inheritence:
Write a TitledPerson class, which you derive from the Person class. The TitledPerson class has one additional String instance variable for a title such as Ms., Mr., or Dr. It has two constructors, a default constructor and one that sets both the name and title. It has a writeOutput method, a reset method, an equals method, an accessor method getTitle that returns the title, and a mutator method setTitle that changes the person’s title. For two titled people to be equal they must have the same name and the same title.
Person.java:
public class Person {
private String name;
public Person(){
name=" ";
}
public Person (String initialName){
name=initialName;
}
//refactor-encapsulate field getSetter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void WriteOutput() {
System.out.println("Name: "+name);
}
public boolean hasSameName(Person other){
return this.name.equalsIgnoreCase(other.name);
//if (this.hasSameName(other))
}
}
2. Create a Sphere class that is derived from your Circle class. Add a method called volume (the volume of a sphere is 4/3 * pi*r*r*r) .
In your main method create two Spheres, print out their circumferences and volumes and then output the larger of the two (you must use compareTo).
double r1 = keyboard.nextDouble();
double r2= keyboard.nextDouble();
Sphere Sphere( r1);
Sphere two = new Sphere(r2);
SYstem.out.println(one.circumference());
System.out.println(one.volume());
//repeat for two
if(r1.compareTo(r2)>0)..
Explanation / Answer
1)
//Person.java
public class Person {
//Attributes
String name;
//Setters and Getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void WriteOutput(){
System.out.println(this.name);
}
// Boolean method
public boolean hasSamePerson(Person p1,Person p2){
if(p1.getName().equals(p2.getName()))
return true;
else
return false;
}
}
//TitlePerson.java
import java.util.Scanner;
public class TitlePerson extends Person{
//Instance Variables
String title;
String name;
//Constructors
public TitlePerson(){
}
public TitlePerson(String name){
this.name=name;
}
public TitlePerson(String name,String title){
this.name=name;
this.title=title;
}
//Setters and Getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public static void main(String args[]){
Scanner s = new Scanner(System.in);
//Reading Title and Name
System.out.println("Enter Title1 and Name1:");
TitlePerson tp1 = new TitlePerson(s.next(),s.next());
System.out.println("Enter Title2 and Name2:");
TitlePerson tp2 = new TitlePerson(s.next(), s.next());
//Comparing Person1 and Person2
//Calling hasSamePerson of Person Type
boolean nb = tp1.hasSamePerson(tp1, tp2);
boolean tb = tp1.getTitle().equals(tp2.getTitle());
//If both are equal then
if(nb && tb){
System.out.println("Equal title and Name");
}else
System.out.println("Unequal");
}
}
Output:
Enter Title1 and Name1:
Mr Jack
Enter Title2 and Name2:
Mr Jack
Equal title and Name
Enter Title1 and Name1:
Mr James
Enter Title2 and Name2:
Ms James
Unequal
2)
//Circle.java
public class Circle {
double radius;
//Constructor
public Circle(double radius){
this.radius = radius;
}
//Calculate Volume
public double getVolume(){
//(4/3)*(22/7)*r*r*r
double volume = (4.188)*radius*radius*radius;
return volume;
}
//Calculate Circumference
public double circumference(){
//2*(22/7)*r
double circum = 6.28*radius;
return circum;
}
}
//Sphere.java
import java.util.Scanner;
public class Sphere extends Circle{
//Calling super class Constructor
public Sphere(double radius){
super(radius);
}
public static void main(String args[]){
//Reading radius
Scanner s = new Scanner(System.in);
System.out.println("Enter Radius:1");
double r1 = s.nextDouble();
//Creating sphere
Sphere sp1 = new Sphere(r1);
//Calling volume ang circumference methods
double vol1 = sp1.getVolume();
double cir1 = sp1.circumference();
System.out.println("Enter Radius:2");
double r2 = s.nextDouble();
Sphere sp2 = new Sphere(r2);
double vol2 = sp2.getVolume();
double cir2 = sp2.circumference();
System.out.println("Sphere1:"+" "+"Volume:"+vol1+" "+"Circumference:"+cir1);
System.out.println("Sphere2:"+" "+"Volume:"+vol2+" "+"Circumference:"+cir2);
//Comparing radius of spheres
if(r1>r2){
System.out.println("Sphere1 is largest");
}else{
System.out.println("Sphere2 is largest");
}
}
}
Output
Enter Radius:1
2.5
Enter Radius:2
3.5
Sphere1: Volume:65.4375 Circumference:15.700000000000001
Sphere2: Volume:179.5605 Circumference:21.98
Sphere2 is largest
Enter Radius:1
4.5
Enter Radius:2
2.7
Sphere1: Volume:381.6315 Circumference:28.26
Sphere2: Volume:82.43240400000002 Circumference:16.956000000000003
Sphere1 is largest
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.