Assignment
University
Stony Brook UniversityCourse
CSE 114 | Introduction to Object-Oriented ProgrammingPages
6
Academic year
2023
Riley
Views
79
CSE114: Introduction to Object Oriented Programming Spring 2022 Homework5 (Extra Credit!): Due Date: Homework4 is due by 11:59 PM EST on Monday, May 10, 2022 . Submit your work (the .java source code files ONLY, not the compiled .class files!) through the “Homework4” link on Blackboard. You may submit an unlimited number of times; we will only grade the last/latest submission attempt, but be sure to attach all of your files to each submission attempt. Be sure to include your name and Stony Brook ID number in a comment at the beginning of each file that you submit. Submission Guidelines: 1. All the files should be inside a folder named as ‘LastName_FirstName_SBUID’ and upload it as zip file. [Example: A student named John Smith with SBUID 100200 will name the folder as Smith_John_100200 and should upload the zipped folder named Smith_John_100200.zip] 2. Each file should be named as HW*problem* (Example: ‘HW2problem1’) 3. If there are multiple classes for a problem, put each problem in a separate folder. Ensure that you compile the program before uploading (as structure mentioned above). If the program does not compile, you will lose points. Instructions: This assignment is worth 40 points (10 + 10 + 10 + 10). 1. Design a class named Triangle that extends GeometricObject class (attached with this Homework). The class contains • Three double data fields named 𝑠𝑖𝑑𝑒 1, 𝑠𝑖𝑑𝑒 2, 𝑎𝑛𝑑 𝑠𝑖𝑑𝑒 3 with default values 1.0 to denote three sides of a triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with specified 𝑠𝑖𝑑𝑒 1, 𝑠𝑖𝑑𝑒 2, 𝑎𝑛𝑑 𝑠𝑖𝑑𝑒 3 . • 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 area of this triangle. • A method named toString() that returns a string description for the triangle. The formula for computing the area of a triangle is: 𝑠 = 𝑠𝑖𝑑𝑒 1 + 𝑠𝑖𝑑𝑒 2 + 𝑠𝑖𝑑𝑒 3 , area = √𝑠 ( 𝑠 − 𝑠𝑖𝑑𝑒 1)( 𝑠 − 𝑠𝑖𝑑𝑒 2)( 𝑠 − 𝑠𝑖𝑑𝑒 3) 2 The toString() method is implemented as follows: 𝑟𝑒𝑡𝑢𝑟𝑛 “𝑇𝑟𝑖𝑎𝑛𝑔𝑙𝑒 : 𝑠𝑖𝑑𝑒 1 = ” + 𝑠𝑖𝑑𝑒 1 + “ 𝑠𝑖𝑑𝑒 2 = ” + 𝑠𝑖𝑑𝑒 2 + “ 𝑠𝑖𝑑𝑒 3 = ” + 𝑠𝑖𝑑𝑒 3 ; 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. 2. Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number and e-mail address. A student has a class status (freshman, sophomore, junior or senior). Define the status as constant. An employee has an office, salary, and experience (number of years, integer value). A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each of the class to display the class name and the person’s name. Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() methods. 3. Write a method that returns an array list of Character from a string using the following header: public static ArrayList<Character> toCharacterArray(String s ) For example toCharacterArray(“abc”) returns an array list that contains characters ‘a’, ‘b’ and ‘c’. Write a test method that prompts the user to enter a String. Call the method toCharacterArray() on the input string. Print the elements of the Character array list separated by exactly one space. Here is a sample run: Enter the input string: Stony Brook Elements of the Character array list: S t o n y B r o o k 4. Given two ArrayLists of sorted strings (alphabetically ascending), write down a method to merge these sorted strings’ ArrayLists into a single sorted ArrayList. The signature of the method is given below: public static ArrayList<String> mergeList(ArrayList<String> lst1 , ArrayList<String> lst2 ) Write down a test method that tests the above method. As an example if: List1 has its elements: “Austin” “Dallas” “San Francisco” and List2 has its elements : “Boston” “Chicago” “Denver” “Seattle” then the merged sorted list should have the elements in the following order : “Austin” “Boston” “Chicago”, “Dallas” “Denver” “San Francisco” “Seattle”
Part 1 Supporting Java Class public class Triangle extends GeometricObject { private double side1 = 1 ; private double side2 = 1 ; private double side3 = 1 ; public Triangle() {} public Triangle( double s1 , double s2 , double s3) { side1 = s1 ; side2 = s2 ; side3 = s3 ; } public double getSide1() { return side1 ; } public double getSide2() { return side2 ; } public double getSide3() { return side3 ; } public double getArea() { double s = (side1+side2+side3)/ 2 ; return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3)) ; } public double getPerimeter() { return side1+side2+side3 ; } public String toString() { return "Triangle:side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3 ; } } Main Java Class import java.util.Scanner ; public class HW5problem1 { public static void main(String[] args) { Scanner stdin = new Scanner(System.in) ; System.out.print( "Enter 3 values for the triangle's sides: " ) ; double s1 = stdin.nextDouble() ; double s2 = stdin.nextDouble() ; double s3 = stdin.nextDouble() ; System.out.print( "Enter the color for the triangle: " ) ; String color = stdin.next() ; System.out.print( "true or false for filled: " ) ; boolean filled = stdin.nextBoolean() ; Triangle t = new Triangle(s1 , s2 , s3) ; t.setColor(color) ; t.setFilled(filled) ; System.out.printf( "Triangle's area is %.2f%n" , t.getArea()) ; System.out.println( "Triangle's perimeter is " + t.getPerimeter()) ; System.out.println( "Triangle's color is " + t.getColor()) ; System.out.println( "Triangle is filled? " + t.isFilled()) ; } }
Part 2 Support Java Classes a) public class Person { private String name , address , phoneNum , email ; public Person() {} public Person(String name , String address , String phone , String email) { this .name = name ; this .address = address ; this .phoneNum = phone ; this .email = email ; } public String getName() { return name ; } public String toString() { return "Class's Name: Person \t Person's Name: " + name ; } } b) public class Student extends Person{ private String status ; public Student() {} public Student(String name , String address , String phone , String email , String status) { super (name , address , phone , email) ; this .status = status ; } public String toString() { return "Class's Name: Student \t Person's Name: " + super .getName() ; } } c) public class Employee extends Person { private String office ; private double salary ; private int experience ; public Employee() {} public Employee(String name , String address , String phone , String email , String office , double salary , int experience) { super (name , address , phone , email) ; this .office = office ; this .salary = salary ; this .experience = experience ; } public String toString() { return "Class's Name: Employee \t Person's Name: " + super .getName() ; } }
d) public class Faculty extends Employee { private String officeHrs , rank ; public Faculty() {} public Faculty(String name , String address , String phone , String email , String office , double salary , int experience , String officeHrs , String rank) { super (name , address , phone , email , office , salary , experience) ; this .officeHrs = officeHrs ; this .rank = rank ; } public String toString() { return "Class's Name: Faculty \t Person's name: " + super .getName() ; } } e) public class Staff extends Employee{ private String title ; public Staff() {} public Staff(String name , String address , String phone , String email , String office , double salary , int experience , String title) { super (name , address , phone , email , office , salary , experience) ; this .title = title ; } public String toString() { return "Class's Name: Staff \t Person's Name: " + super .getName() ; } } Main Java Class public class HW5problem2 { public static void main(String[] args) { Person p = new Person( "Bob" , "123 street" , "555-5555" , "[email protected]" ) ; Student st = new Student( "John" , "123 street" , "555-5555" , "[email protected]" , "freshmen" ) ; Employee e = new Employee( "Sara" , "123 street" , "555-5555" , "[email protected]" , "test office" , 50000 , 5 ) ; Faculty f = new Faculty( "Jacky" , "123 street" , "555-5555" , "[email protected]" , "test office" , 50000 , 5 , "9:30 am to 11:30 am" , "supervisor" ) ; Staff s = new Staff( "Sam" , "123 street" , "555-5555" , "[email protected]" , "test office" , 50000 , 5 , "typist" ) ; System.out.println(p) ; System.out.println(st) ; System.out.println(e) ; System.out.println(f) ; System.out.println(s) ; } }
Part 3 import java.util.* ; public class HW5problem3 { public static void main(String[] args) { Scanner stdin = new Scanner(System.in) ; System.out.print( "Enter a string: " ) ; String s = stdin.nextLine() ; for (Character c : toCharacterArray(s)) System.out.print(c + " " ) ; } public static ArrayList<Character> toCharacterArray(String s) { ArrayList<Character> c = new ArrayList<>() ; for ( int i = 0 ; i < s.length() ; i++) { c.add(s.charAt(i)) ; } return c ; } } Part 4 import java.util.ArrayList ; import java.util.Arrays ; public class HW5problem4 { public static void main(String[] args) { ArrayList<String> l1 = new ArrayList<>(Arrays.asList( "Austin" , "Dallas" , "San Francisco" )) ; ArrayList<String> l2 = new ArrayList<>(Arrays.asList( "Boston" , "Chicago" , "Denver" , "Seattle" )) ; System.out.print( "List 1: " ) ; for (String s : l1) System.out.print(s + ", " ) ; System.out.println() ; System.out.print( "List 2: " ) ; for (String s : l2) System.out.print(s + ", " ) ; System.out.println() ; System.out.print( "Merged List: " ) ; for (String s : mergeList(l1 , l2)) System.out.print(s + ", " ) ; } public static ArrayList<String> mergeList(ArrayList<String> lst1 , ArrayList<String> lst2) { ArrayList<String> newList = new ArrayList<>() ; int n1 = 0 ; int n2 = 0 ; while (n1 < lst1.size() && n2 < lst2.size()) { if (lst1.get(n1).compareTo(lst2.get(n2)) < 0 ) { newList.add(lst1.get(n1)) ; n1++ ; } else { newList.add(lst2.get(n2)) ; n2++ ; } } if (n1 == lst1.size()) for ( int i = n2 ; i < lst2.size() ; i++) newList.add(lst2.get(i)) ; else for ( int i = n1 ; i < lst1.size() ; i++) newList.add(lst1.get(i)) ; return newList ; } }
CSE114: Spring 2022 Homework 5
Please or to post comments