Assignment
University
Stony Brook UniversityCourse
CSE 114 | Introduction to Object-Oriented ProgrammingPages
8
Academic year
2023
Riley
Views
22
CSE114: Introduction to Object Oriented Programming Spring 2022 Homework4: Due Date: Homework4 is due by 11:59 PM EST on Monday, April 26, 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. Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form 𝑟𝑒𝑎𝑙𝑃𝑎𝑟𝑡 + 𝑖𝑚𝑎𝑔𝑖𝑛𝑎𝑟𝑦𝑃𝑎𝑟𝑡 ∗ 𝑖 where 𝑖 is √− 1 Write a program to test your class. Use double variable to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform the following operations: a) Add two Complex numbers: The real parts are added together and the imaginary parts are added together. b) Subtract two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand. c) Print Complex numbers in the form ( 𝑎 , 𝑏 ) where 𝑎 the real part and 𝑏 is the imaginary part. [10] Here is a sample run: Enter real part of the first complex number: 3 Enter imaginary part of the first complex number: 6
Enter real part of the second complex number: 2 Enter imaginary part of the second complex number: 7 First complex number is: (3.0, 6.0) Second complex number is: (2.0, 7.0) Addition of the complex numbers is: (5.0, 13.0) Subtraction of the complex numbers is: (1.0, -1.0) 2. Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class - the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object as 1 in the numerator and 2 in the denominator. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform each of the following operations: a) Add two Rational numbers: The result of the addition should be stored in reduced form. b) Subtract two Rational numbers: The result of subtraction should be stored in reduced form. c) Multiply two Rational numbers: The result of the multiplication should be stored in reduced form. d) Divide two Rational numbers: The result of the division should be stored in reduced form. e) Return a String representation of Rational number in the form 𝑎 / 𝑏 , where 𝑎 is the numerator and 𝑏 is the denominator. Here is a sample run: Enter numerator for the first rational number: 4 Enter a non-zero denominator for the first rational number: 6 Enter numerator for the second rational number: 1 Enter a non-zero denominator for the second rational number: 4 First rational number is: 2/3 Second rational number is: 1/4 Addition of the rational numbers is: 11/12 Subtraction of the rational numbers is: 5/12 Multiplication of the rational numbers is: 1/6 Division of the rational numbers is: 8/3 [10] 3. Define the Circle2D class that contains: • Two double data fields named x and y that specify the center of the circle with getter methods. • A data field radius with a getter method.
P • A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. • A constructor that creates a circle with the specified x, y, and radius. • A method getArea() that returns the area of the circle. • A method getPerimeter() that returns the perimeter of the circle. • A method contains(double x, double y) that returns true if the specified point (x, y) is inside the circle (see the figure below). • A method contains (Circle2D circle) that returns true if the specified circle is inside this circle (see the figure below). • A method overlaps(Circle2D circle) that returns true if the specified circle overlaps with this circle (see the figure below). Write a test program that creates a Circle2D object c1 with its x, y coordinates and the radius as 2, 2, and 5.5, respectively. Display the area and the perimeter of c1. Display the result of c1.contains(3, 3), c1.contains (new Circle2D(4, 5, 10.5)), and c1.overlaps(new Circle2D(3, 5, 2.3)). (a) (b) (c) Figure: (a): A point inside the circle. (b): A circle inside another circle. (c): A circle overlaps another circle. [10] 4. Implement the following method to sort (in ascending order) the rows in a two-dimensional array. A new array is returned and the original array is intact. public static double[][] sortRows(double[][] m) Write a test program that prompts the user to enter a 3 × 3 matrix of double values and displays a new row-sorted matrix. Here is a sample run: [10] Enter a 3-by-3 matrix row by row: 0.15 0.875 0.375 0.55 0.005 0.225 0.30 0.12 0.4 The row-sorted array is: 0.15 0.375 0.875 0.005 0.225 0.55 0.12 0.30 0.4 [Note: Please implement your own sorting algorithm. You can implement Bubble sort.]
Part 1 Supporting Java Class public class Complex { private double real ; private double imaginary ; public Complex() { real = 2 ; imaginary = 3 ; } public Complex( double r , double i) { real = r ; imaginary = i ; } public double [] add(Complex num2) { double [] addResult = new double [ 2 ] ; addResult[ 0 ] = real + num2.real ; addResult[ 1 ] = imaginary + num2.imaginary ; return addResult ; } public double [] subtract(Complex num2) { double [] subResult = new double [ 2 ] ; subResult[ 0 ] = real - num2.real ; subResult[ 1 ] = imaginary - num2.imaginary ; return subResult ; } public void printNum() { System.out.printf( "(%.1f, %.1f)%n" , real , imaginary) ; } } Main Java Class import java.util.Scanner ; public class HW4problem1 { public static void main(String[] args) { Scanner stdin = new Scanner(System.in) ; System.out.print( "Enter real part of the first complex number: " ) ; double r1 = stdin.nextDouble() ; System.out.print( "Enter imaginary part of the first complex number: " ) ; double i1 = stdin.nextDouble() ; System.out.print( "Enter real part of the second complex number: " ) ; double r2 = stdin.nextDouble() ; System.out.print( "Enter imaginary part of the second complex number: " ) ; double i2 = stdin.nextDouble() ; Complex num1 = new Complex(r1 , i1) ; Complex num2 = new Complex(r2 , i2) ; System.out.print( "First complex number is: " ) ; num1.printNum() ; System.out.print( "Second complex number is: " ) ; num2.printNum() ; double [] addResult = num1.add(num2) ; System.out.printf( "Addition of the complex numbers is: (%.1f, %.1f)%n" , addResult[ 0 ] , addResult[ 1 ]) ; double [] subResult = num1.subtract(num2) ; System.out.printf( "Subtraction of the complex numbers is: (%.1f, %.1f)%n" , subResult[ 0 ] ,
subResult[ 1 ]) ; } } Part 2 Supporting Java Class public class Rational { private int n , d ; public Rational() { n = 1 ; d = 2 ; } public Rational( int num , int deno) { int [] temp = {num , deno} ; simplify(temp) ; n = temp[ 0 ] ; d = temp[ 1 ] ; } public int [] add(Rational num2) { int [] addResult = new int [ 2 ] ; addResult[ 0 ] = n*num2.d + num2.n*d ; addResult[ 1 ] = d*num2.d ; simplify(addResult) ; return addResult ; } public int [] subtract(Rational num2) { int [] subResult = new int [ 2 ] ; subResult[ 0 ] = n*num2.d - num2.n*d ; subResult[ 1 ] = d*num2.d ; simplify(subResult) ; return subResult ; } public int [] multiply(Rational num2) { int [] multiplyResult = new int [ 2 ] ; multiplyResult[ 0 ] = n*num2.n ; multiplyResult[ 1 ] = d*num2.d ; simplify(multiplyResult) ; return multiplyResult ; } public int [] divide(Rational num2) { int [] divideResult = new int [ 2 ] ; divideResult[ 0 ] = n*num2.d ; divideResult[ 1 ] = d*num2.n ; simplify(divideResult) ; return divideResult ; } public String toString() { return n + "/" + d ; } private void simplify( int [] n) { int factor = 1 ; int min = Math.min(n[ 0 ] , n[ 1 ]) ; int max = Math.max(n[ 0 ] , n[ 1 ]) ; for ( int i = min ; i >= 2 ; i--) {
if (max%i == 0 && min%i == 0 ) { factor = i ; break; } } n[ 0 ] /= factor ; n[ 1 ] /= factor ; } } Main Java Class import java.util.Scanner ; public class HW4problem2 { public static void main(String[] args) { Scanner stdin = new Scanner(System.in) ; System.out.print( "Enter numerator for the first rational number: " ) ; int n1 = stdin.nextInt() ; System.out.print( "Enter a non-zero denominator for the first rational number: " ) ; int d1 = stdin.nextInt() ; System.out.print( "Enter numerator for the second rational number: " ) ; int n2 = stdin.nextInt() ; System.out.print( "Enter a non-zero denominator for the second rational number: " ) ; int d2 = stdin.nextInt() ; Rational num1 = new Rational(n1 , d1) ; Rational num2 = new Rational(n2 , d2) ; System.out.println( "First rational number is: " + num1) ; System.out.println( "Second rational number is: " + num2) ; int [] addResult = num1.add(num2) ; System.out.printf( "Addition of the rational numbers is: %d/%d%n" , addResult[ 0 ] , addResult[ 1 ]) ; int [] subResult = num1.subtract(num2) ; System.out.printf( "Subtraction of the rational numbers is: %d/%d%n" , subResult[ 0 ] , subResult[ 1 ]) ; int [] multiplicationResult = num1.multiply(num2) ; System.out.printf( "Multiplication of the rational numbers is: %d/%d%n" , multiplicationResult[ 0 ] , multiplicationResult[ 1 ]) ; int [] divisionResult = num1.divide(num2) ; System.out.printf( "Division of the rational numbers is: %d/%d%n" , divisionResult[ 0 ] , divisionResult[ 1 ]) ; } }
Part 3 Supporting Java Class public class Circle2D { private double x , y , radius ; public Circle2D() { x = 0 ; y = 0 ; radius = 1 ; } public Circle2D( double xpoint , double ypoint , double r) { x = xpoint ; y = ypoint ; radius = r ; } public double getArea() { return Math.PI*radius*radius ; } public double getPerimeter() { return 2 *Math.PI*radius ; } public boolean contains( double i , double j) { if (i >= x-radius && i <= x+radius && j >= y-radius && j <= y+radius) return true; return false; } public boolean contains(Circle2D c) { double i = c.x ; double j = c.y ; double r = c.radius ; if (i-r > x-radius && i+r < x+radius && j-r > y-radius && j+r < y+radius) return true; return false; } public boolean overlaps(Circle2D c) { double i = c.x ; double j = c.y ; double r = c.radius ; boolean xIsContained = (i-r > x-radius && i-r < x+radius) || (i+r > x-radius && i+r < x+radius) ; boolean yIsContained = (j-r > y-radius && j-r < y+radius) || (j+r > y-radius && j+r < y+radius) ; if (xIsContained && yIsContained) return true; return false; } public double getX() { return x ; } public double getY() { return y ; } public double getRadius() { return radius ; } }
Main Java Class public class HW4problem3 { public static void main(String[] args) { Circle2D c1 = new Circle2D( 2 , 2 , 5.5 ) ; System.out.printf( "Area of c1 is: %.3f%n" , c1.getArea()) ; System.out.printf( "Perimeter of c1 is: %.3f%n" , c1.getPerimeter()) ; System.out.println( "Does c1 contains the point(3,3)? " + c1.contains( 3 , 3 )) ; System.out.println( "Does c1 contains the circle(4, 5, 10.5)? " + c1.contains( new Circle2D( 4 , 5 , 10.5 ))) ; System.out.println( "Does c1 overlaps with the circle(3, 5, 2.3)? " + c1.overlaps( new Circle2D( 3 , 5 , 2.3 ))) ; } } Part 4 import java.util.Scanner ; public class HW4problem4 { public static void main(String[] args) { Scanner stdin = new Scanner(System.in) ; System.out.println( "Enter a 3-by-3 matrix row by row:" ) ; double [][] m = new double [ 3 ][ 3 ] ; for ( int i = 0 ; i < m.length ; i++) for ( int j = 0 ; j < m[i].length ; j++) m[i][j] = stdin.nextDouble() ; System.out.println() ; double [][] sortedm = sortRows(m) ; System.out.println( "The row-sorted array is:" ) ; for ( int i = 0 ; i < sortedm.length ; i++) { for ( double j : sortedm[i]) { System.out.printf( "%.3f " , j) ; } System.out.println() ; } } public static double [][] sortRows( double [][] m) { double [][] m1 = new double [m.length][m[ 0 ].length] ; for ( int i = 0 ; i < m.length ; i++) for ( int j = 0 ; j < m[i].length ; j++) m1[i][j] = m[i][j] ; for ( int i = 0 ; i < m1.length ; i++) for ( int j = 0 ; j < m1[i].length ; j++) for ( int k = 0 ; k < m1[i].length- 1 ; k++) if (m1[i][k] > m1[i][k+ 1 ]) { double temp = m1[i][k] ; m1[i][k] = m1[i][k+ 1 ] ; m1[i][k+ 1 ] = temp ; } return m1 ; } }
CSE114: Spring 2022 Homework 4
Please or to post comments