CS114: Introduction to Java Fall 2015, Mon/Wed, 5:30 PM - 6:45 PM Instructor: Pejman Ghorbanzade to Assignment 4 Release Date: Nov 04, 2015 at 5:30 PM Due Date: Nov 18, 2015 at 5:30 PM Question 1 An n n matrix is called a positive Markov matrix if each element is positive and the sum of the elements in each column is 1. Write a program MarkovMatrix.java that prompts the user to enter a 3 3 matrix of double values. Use a method with the following signature to test if the given matrix is a Markov matrix. public static boolean ismarkovmatrix(double[][] matrix) Your program is expected to function as shown in following examples: $ javac MarkovMatrix.java $ java MarkovMatrix Enter Row 1: 0.15 0.875 0.375 Enter Row 2: 0.55 0.005 0.225 Enter Row 3: 0.30 0.12 0.4 Markov matrix given. CS114: Introduction to Java, Fall 2015 1
2 public class MarkovMatrix { 4 double[][] matrix = initmatrix(3, 3); 5 if (ismarkovmatrix(matrix)) 6 System.out.println("Markov matrix given."); 7 else 8 System.out.println("Matrix not Markov."); 9 } 10 public static double[][] initmatrix(int row, int col) { 11 Scanner input = new Scanner(System.in); 12 double[][] matrix = new double[row][col]; 13 for (int i = 0; i < row; i++) { 14 System.out.printf("Enter Row %d: ", i + 1); 15 for (int j = 0; j < col; j++) 16 matrix[i][j] = input.nextdouble(); 17 } 18 input.close(); 19 return matrix; 20 } 21 public static boolean ismarkovmatrix(double[][] matrix) { 22 for (int i = 0; i < matrix[0].length; i++) { 23 double sum = 0; 24 for (int j = 0; j < matrix.length; j++) 25 sum += matrix[j][i]; 26 if (sum!= 1) 27 return false; 28 } 29 return true; 30 } 31 } 2 CS114: Introduction to Java, Fall 2015
Question 2 Write a program PointAndSphere2.java that prompts user to enter respectively, coordinates of a point, coordinates of the center of a sphere and radius of the sphere. Your program would then determine the location of the point with respect to the sphere. The point should be an instance of class Point and the sphere should be an instance of class Sphere. Following is an example of an accepted output format. $ javac Point.java Sphere.java PointAndSphere2.java $ java PointAndSphere2 Coordinates of Point: 1 1 1 Coordinates of Sphere: 0 0 0 Radius of Sphere: 1.7 The point is outside the sphere. 1 public class Point { 2 public double[] coordinates; 3 public Point(double[] coordinates) { 4 this.coordinates = coordinates; 5 } 6 public double getdistance(point point) { 7 double sum = 0; 8 for (int i = 0; i < 3; i++) 9 sum += Math.pow(this.coordinates[i] - point.coordinates[i], 2); 10 return Math.sqrt(sum); 11 } 12 } 1 public class Sphere { 2 public Point center; 3 public double radius; 4 public Sphere(Point center, double radius) { 5 this.center = center; 6 this.radius = radius; 7 } 8 } CS114: Introduction to Java, Fall 2015 3
2 public class PointAndSphere2 { 4 double[] pointcoordinates = promptcoordinates("point"); 5 double[] spherecoordinates = promptcoordinates("sphere"); 6 double radius = promptradius(); 7 Point point = new Point(pointCoordinates); 8 Point center = new Point(sphereCoordinates); 9 Sphere sphere = new Sphere(center, radius); 10 double dist = sphere.center.getdistance(point); 11 if (dist > sphere.radius) 12 System.out.println("The point is outside the sphere."); 13 else if (dist == sphere.radius) 14 System.out.println("The point is on the sphere."); 15 else 16 System.out.println("The point is inside the sphere."); 17 } 18 public static double[] promptcoordinates(string name) { 19 Scanner input = new Scanner(System.in); 20 System.out.printf("Coordinates of %s: ", name); 21 double[] coordinates = new double[3]; 22 for (int i = 0; i < coordinates.length; i++) 23 coordinates[i] = input.nextdouble(); 24 return coordinates; 25 } 26 public static double promptradius() { 27 Scanner input = new Scanner(System.in); 28 System.out.print("Radius of Sphere: "); 29 double radius = input.nextdouble(); 30 return radius; 31 } 32 } 4 CS114: Introduction to Java, Fall 2015
Question 3 Write a program MatrixFiller2.java that prompts user for a number x between 1 to 9 and instantiates an x x matrix from class Matrix.java whose elements are randomly generated from range 1 to x 2. Following is an expected sample run of your program. $ javac Matrix.java MatrixFiller2.java $ java MatrixFiller2 Size of Matrix: 4 05 13 07 16 12 02 10 01 09 14 14 08 02 05 01 14 1 public class Matrix { 2 int[][] elements; 3 public Matrix(int row, int col) { 4 this.elements = new int[row][col]; 5 for (int i = 0; i < row; i++) 6 for (int j = 0; j < col; j++) { 7 int rand = (int) (Math.random() * row * col) + 1; 8 this.elements[i][j] = rand; 9 } 10 } 11 public void display() { 12 int row = this.elements.length; 13 int col = this.elements[0].length; 14 for (int i = 0; i < row; i++) { 15 for (int j = 0; j < col; j++) 16 System.out.printf(" %02d", elements[i][j]); 17 System.out.println(); 18 } 19 } 20 } CS114: Introduction to Java, Fall 2015 5
2 public class MatrixFiller2 { 4 Scanner input = new Scanner(System.in); 5 System.out.print("Size of Matrix: "); 6 int size = input.nextint(); 7 input.close(); 8 Matrix matrix = new Matrix(size, size); 9 matrix.display(); 10 } 11 } Question 4 Write a class Circle.java from which we can instantiate a circle by giving its radius and use it as is shown in the following program. 2 public class Circles { 4 Scanner input = new Scanner(System.in); 5 System.out.print("Enter radius: "); 6 double radius = input.nextdouble(); 7 input.close(); 8 Circle mycircle = new Circle(radius); 9 double area = mycircle.getarea(); 10 double perimeter = mycircle.getcircumference(); 11 System.out.printf("Area: %.2f, Perimeter: %.2f\n", area, perimeter); 12 } 13 } 1 public class Circle { 2 double radius; 3 public Circle(double radius) { 4 this.radius = radius; 5 } 6 public double getarea() { 7 double area = Math.PI * Math.pow(this.radius, 2); 8 return area; 6 CS114: Introduction to Java, Fall 2015
9 } 10 public double getcircumference() { 11 double circumference = 2 * Math.PI * this.radius; 12 return circumference; 13 } 14 } Question 5 The code snippet given below is content of a file Kitten.java found in a public repository. Unfortunately, the program cannot be executed because the file Cat.java which defines the class Cat is missing. You are expected to develop the class Cat in a file Cat.java such that Kitten.java is successfully executed. 2 public class Kitten { 4 Cat mycat = new Cat("Kitty"); 5 double[] movement = promptmove(mycat); 6 mycat.move(movement[0], movement[1]); 7 mycat.showposition(); 8 mycat.showdistance(); 9 } 10 public static double[] promptmove(cat mycat) { 11 Scanner input = new Scanner(System.in); 12 char[] directions = { X, Y }; 13 double[] movement = new double[directions.length]; 14 for (int i = 0; i < directions.length; i++) { 15 System.out.printf("Distance to move in %c direction: ", directions[i]); 16 movement[i] = input.nextdouble(); 17 } 18 input.close(); 19 return movement; 20 } 21 } CS114: Introduction to Java, Fall 2015 7
Following is a sample expected run of the program. $ javac Cat.java Kitten.java $ java Kitten Distance to move in X direction: 3 Distance to move in Y direction: 4 Kitty is in (3.0, 4.0). Kitty is 5.00 units away from (0, 0). 1 public class Cat { 2 public String name; 3 public double dirx = 0; 4 public double diry = 0; 5 public Cat(String name) { 6 this.name = name; 7 } 8 public void move(double dirx, double diry) { 9 this.dirx += dirx; 10 this.diry += diry; 11 } 12 public void showposition() { 13 System.out.printf("%s is in (%.1f, %.1f).\n", this.name, this.dirx, this.diry); 14 } 15 public void showdistance() { 16 double distance = Math.sqrt(Math.pow(this.dirX, 2) + Math.pow(this.dirY, 2)); 17 System.out.printf("%s is %.2f units away from (0, 0).\n", this.name, distance); 18 } 19 } 8 CS114: Introduction to Java, Fall 2015