CS114: Introduction to Java



Similar documents
Part I. Multiple Choice Questions (2 points each):

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

J a v a Quiz (Unit 3, Test 0 Practice)

(Eng. Hayam Reda Seireg) Sheet Java

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Sample CSE8A midterm Multiple Choice (circle one)

Introduction to Java. CS 3: Computer Programming in Java

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Introduction to Java

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Building Java Programs

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Topic 11 Scanner object, conditional execution

LOOPS CHAPTER CHAPTER GOALS

The following program is aiming to extract from a simple text file an analysis of the content such as:

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Using Files as Input/Output in Java 5.0 Applications

CS170 Lab 11 Abstract Data Types & Objects

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Chapter 2 Introduction to Java programming

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

AP Computer Science Java Mr. Clausen Program 9A, 9B

Two-Dimensional Arrays Summer 2010 Margaret Reid-Miller

Arrays. Introduction. Chapter 7

Chapter 2 Elementary Programming

Building Java Programs

CSE 1020 Introduction to Computer Science I A sample nal exam

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

AP Computer Science Java Subset

JAVA ARRAY EXAMPLE PDF

Java Crash Course Part I

Programmierpraktikum

Crash Course in Java

Example of a Java program

CS 106 Introduction to Computer Science I

Iteration CHAPTER 6. Topic Summary

Building Java Programs

java Features Version April 19, 2013 by Thorsten Kracht

Block IQ. Marko Boon Jacques Resing

Building Java Programs

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Lab 5: Bank Account. Defining objects & classes

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

AP Computer Science File Input with Scanner

In this Chapter you ll learn:

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Collections.sort(population); // Método de ordenamiento

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

More on Objects and Classes

13 File Output and Input

CSE 8B Midterm Fall 2015

AP Computer Science Static Methods, Strings, User Input

Java Programming Fundamentals

Moving from CS 61A Scheme to CS 61B Java

Chapter 2: Elements of Java

Inheritance, overloading and overriding

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Data Structures Lecture 1

PIC 10A. Lecture 7: Graphics II and intro to the if statement

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Object-Oriented Programming in Java

Supporting Data Set Joins in BIRT

Java Programming Unit 1. Your first Java Program Eclipse IDE

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Arrays in Java. Working with Arrays

Java 進 階 程 式 設 計 03/14~03/21

Line-based file processing

Lecture 1 Introduction to Java

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Introduction to Java Lecture Notes. Ryan Dougherty

While and Do-While Loops Summer 2010 Margaret Reid-Miller

CS 111 Classes I 1. Software Organization View to this point:

Install Java Development Kit (JDK) 1.8

Building Java Programs

Introduction to Computer Programming (CS 1323) Project 9

Homework/Program #5 Solutions

CASCADING IF-ELSE. Cascading if-else Semantics. What the computer executes: What is the truth value 1? 3. Execute path 1 What is the truth value 2?

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Informatica e Sistemi in Tempo Reale

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

Conditional Statements Summer 2010 Margaret Reid-Miller

Interactive Applications (CLI) and Math

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

Programming Fundamentals I CS 110, Central Washington University. November 2015

Basics of Java Programming Input and the Scanner class

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Transcription:

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