CS114: Introduction to Java
Fall 2015, Mon/Wed, 5:30 PM - 6:45 PMInstructor: Pejman Ghorbanzade
Solution 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 pos-itive and the sum of the elements in each column is 1. Write a program MarkovMatrix.javathat 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.
1 import java.util.Scanner; 2 public class MarkovMatrix {
3 public static void main(String[] args) { 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 }
Question 2
Write a program PointAndSphere2.java that prompts user to enter re-spectively, 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 classPoint and the sphere should be an instance of classSphere. 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.
Solution
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 }
2 public class PointAndSphere2 {
3 public static void main(String[] args) {
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 }
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 x2. 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
Solution
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++)
2 public class MatrixFiller2 {
3 public static void main(String[] args) { 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.javafrom which we can instantiate a circle by giving its radius and use it as is shown in the following program.
1 import java.util.Scanner; 2 public class Circles {
3 public static void main(String[] args) { 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 }
Solution
1 public class Circle { 2 double radius;
3 public Circle(double radius) { 4 this.radius = radius;
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 suc-cessfully executed.
1 import java.util.Scanner; 2 public class Kitten {
3 public static void main(String[] args) { 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]);
$ 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).
Solution
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 }