Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Implement problems 1 and 2 as stated below. Provide a sample execution of cach that tests each of the programs, along with each of the
Implement problems 1 and 2 as stated below. Provide a sample execution of cach that tests each of the programs, along with each of the java files. Each problem is worth 10 points. Submit sample executions and class files online through lab 3. Place them all in lab03 project 1. (The Triangle class) Design a class named Triangle that extends GeometricObject (see attached). The class contains: .Three double data fields named sidel, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle A constructor that creates a triangle with the specified sideI, side2, and side3 The accessor methods for all three data fields. A method named getArea) that returns the area of this triangle. .A method named getPerimeter) that returns the perimeter of this triangle. .A method named toString0 that returns a string description for the triangle For the formula to compute the area of a triangle, see Programming Exercise 2.19 The toString) method is implemented as follows: return "Triangle: sidelsidel+"side2-side2+ side3"+sidc3 Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not. Repeat the prompt until the user enters a number less than or equal to zero. Your sample run should show at least three repetitions. 2. (The Course class) Rewrite the Course class in Listing 10.6 p. 377. Use an ArrayList rather than an array to store students. Do not change the original contract of the Course class (i.e., the definition of the constructors and methods should not be changed, but the private members may be changed.) Use TestCourse (Listing 10.6 below) to test the changes that you made with one slight alteration. Drop a student and then run print the array again. There should be no changes required to list 10.6 for it to work. public class GeometricObject f private String color "white" private boolean filled: private java.util.Date dateCreated: Construct a default geometric object/ public GcometricObjectO dateCreated new java.util.Date0 Constr *and filled value ruct a geometric object with the specified color public GcometricObject(String color, boolean filled) dateCreated- new java.util. Date): this.color color this.filled filled: Return color public String getColorO return color; Set a new color* public void setColor(String color) this.color color; * Return filled. Since filled is boolean its get method is named isFilled/ public boolean isFilledO return filled; Set a new fillcd public void setFilled(boolean filled) this.filled filled; *Get dateCreated public java.util. Date getDateCreatedo t return dateCreated; Return a string representation of this object public String toString0 return "created on"+dateCreated"ncolorcolor+ and filled: "+filled LISTING 10.6 Course.java 1 public class Course t 2 private String courseName; 3 private String[] students new String[100]; 4 private int numberOfStudents; 6 public Course(String courseName) this.courseName courseName: 10 public void addStudent(String student) [ students [numberOfStudents] student numberOfStudents++ 12 13 14 15 public String[] getStudentsO 16 17 18 19 public int getNumberOfStudents) f 20 21 return students return numberOfStudents 23 public String getCourseName O 24 25 26 27 public void dropStudent (String student) 28 29 30 return courseName // Left as an exercise in Programming Exercise 10.9 LISTING 10.5 TestCourse.java 1 public class TestCourse 2 public static void main(String[] args) Course coursel new Course("Data Structures" Course course2new Course ("Database Systems) 4 6 coursel.addStudent("Peter Jones"); coursel.addStudent("Kim Smith"); coursel.addStudent ("Anne Kennedy" 9 10 course2.addStudent("Peter Jones"; course2.addStudent ( Steve Smith"; 12 13 14 15 16 17 18 19 20 21 System.out.println("Number of students in coursel:" +coursel.getNumberOfStudents O) Stringl] students coursel.getStudents O for (int i =0; 1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started