Use Java for the following: 1) Design a class named Triangle that extends Geomet
ID: 663605 • Letter: U
Question
Use Java for the following:
1) Design a class named Triangle that extends GeometricObject:
import java.util.Scanner;
abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject() {
}
/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean ,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
@Override
public String toString() {
return "created on " + dateCreated + " color: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}
The Triangle class contains:
Three double data fields named side1, side2, and side3
A default constructor that creates a triangle with three sides of length 1.0
A constructor that creates a triangle with specified values for side1, side2, and side3
Accessor methods for all three data fields
A method called getArea() that returns the area of a triangle
A method named getPerimeter() that returns the perimeter of the triangle
A method named toString() that returns the string description of the triangle in the following format: "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;
Test your Triangle class in a Drive program (in the same file) that prompts the user to enter the three sides of the triangle, the color, and whether or not the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties. Then, it should display the area, perimeter, color, and filled value .
2) Design a clas named Person and its two subclasses, Student and Employee. Make Faculty and Staff subclasses of Employee.
A Person object has a name , address, phone number, and email address (all Strings ).
A Student has a class status (freshman, sophomore, junior, or senior). Define the status as a final String variable .
An Employee has an office number , salary (both ints ), and a date hired. Use the MyDate class defined below to create an object for date hired:
class MyDate{
private String date; //date in the form mm/dd/yy
public MyDate(String date){
this.date = date;
}
public String getDate(){
return date;
}
}
A Faculty object has office hours and a rank (both Strings ), while a Staff object has a title (as a String ).
For the Student, Faculty, and Staff classes, create toString methods that store information about the object (in the format shown in the examples below).
Test your classes in a Driver class (within the same file) that asks the user what type of object they'd to create as well as what information they'd like it to have. The program then uses the object 's toString method to print information about that object .
Explanation / Answer
The solution of Question 1:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package geometricobject;
import java.util.Scanner;
/**
*
* @author anurag
*/
abstract class GeometricObject
{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject()
{
}
/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled)
{
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor()
{
return color;
}
/** Set a new color */
public void setColor(String color)
{
this.color = color;
}
/** Return filled. Since filled is boolean ,
* the get method is named isFilled */
public boolean isFilled()
{
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled)
{
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated()
{
return dateCreated;
}
@Override
public String toString() {
return "created on " + dateCreated + " color: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
/**
* @param args the command line arguments
*/
}
class Triangle extends GeometricObject
{
public double side1;
public double side2;
public double side3;
public Triangle() //default constructor
{
side1=1.0;
side2=1.0;
side3=1.0;
}
public Triangle(double side1,double side2,double side3) //peremetrised constructor
{
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
@Override
public double getPerimeter() //function for getting peremeter for triangle
{
return(side1+side2+side3) ;
}
@Override
public double getArea() // function for getting area of triangle
{
double area,s;
s=side1+side2+side3;
area = Math.sqrt(s * (s- side1) * (s - side2) * (s - side3));
return(area);
}
@Override
public String toString()//to print the data
{
String s;
s="Triangle:side1"+side1+"side2"+side2+"side3"+side3;
return(s);
}
public static void main(String args[])
{
Triangle t=new Triangle(2,3,4); //passing values to peremeterised constructor
System.out.println(t.getPerimeter());
System.out.println(t.getArea());
System.out.println(t.toString());
}
}
sample output:
9.0
43.474130238568314
Triangle:side12.0side23.0side34.0
BUILD SUCCESSFUL (total time: 1 second)
The solution of 2nd Question:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication11;
/**
*
* @author anurag
*/
import java.util.Scanner;
/**
*
* @author anurag
*/
class person
{
String name;
String addr;
String phone_no;
String email;
}
class student extends person
{
final String status="senior" ; //status is one of freshman, sophomore, junior, or senior
}
class employee extends person
{
public int office_no;
public int salery;
public int date_hired;
}
class MyDate
{
private String date; //date in the form mm/dd/yy
public MyDate(String date)
{
this.date = date;
}
public String getDate()
{
return date;
}
}
class faculty extends employee
{
String office_hrs="9";
String rank="3";
}
class staff extends employee
{
String title="Mr.xyz";
}
class ravi // this is the class for showing the results.
{
public void tostring(int i)
{
if(i==1)
{
student st=new student();
System.out.println(st.status);
}
if(i==2)
{
faculty ft=new faculty();
System.out.println(ft.office_hrs);
System.out.println(ft.rank);
}
if(i==3)
{
staff sf=new staff();
System.out.println(sf.title);
}
}
}
public class JavaApplication11
{
public static void main(String[] args)
{
int choice; //choice 1 forstudent info,2 for faculty info,3 for staff info.
System.out.println("enter choice:");
Scanner sc =new Scanner(System.in) ;
choice=sc.nextInt();
ravi r=new ravi();
r.tostring(choice);
}
}
sample output:
enter choice:
1
senior
BUILD SUCCESSFUL (total time: 5 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.