CMPS 200 Homework Assignment 13, Fall 2011

American University of Beirut · Introduction to Programming · 4 pages

All CMPS 200 files
CMPS 200 Homework Assignment 13, Fall 2011, page 1

Stuck on a question? A tutor can walk you through it.

CMPS 200 Homework Assignment 13, Fall 2011, page 2 CMPS 200 Homework Assignment 13, Fall 2011, page 3 CMPS 200 Homework Assignment 13, Fall 2011, page 4

More CMPS 200 files

Show the text of this homework
Faculty of Arts & Sciences Department of Computer Science CMPS 200—Introduction to Programming Assignment 13 – Due Friday Jan 13, 2012 Reading Material: • Chapter 6. The chapter covers the built-in Java I/O datatypes including the File, Scanner, and PrintStream datatypes. Note: In this assignment, all I/O must be done with the standard Java I/O APIs i.e. File, Scanner and PrintStream. You may not use the In, Out, and related classes we used in previous assignments. 1. Rotating Sentences You are asked to rotate a series of input sentences 90 degrees clockwise, i.e., instead of displaying them from left to right and top to bottom, your program will display them from top to bottom and right to left. The program will take the names of the input and output files as command line arguments. The file can contain a maximum of 100 sentences, each not exceeding 100 characters long. Legal characters include: newline, space, any punctuation characters, digits, and lower case or upper case English letters. (NOTE: Tabs are not legal characters.) The output file should have the last sentence printed out vertically in the leftmost column; the first sentence of the input would subsequently end up in the rightmost column. Sample Input 1: Rene Decartes once said, "I think, therefore I am." Sample Output 1: "R Ie n te h iD ne kc ,a r tt he es r eo fn oc re e s Ia i ad m, . " Introduction to Programming (CMPS 200) Fall 2011 2 of 4 2. Stacks Class StackArray A stack is a basic data structure, where insertion and deletion of items takes place at one end called top of the stack. A Stack is mainly characterized by three fundamental operations: push, pop and peek (described below). Implement a class named StackArray with the following properties: StackArray - int[] s; //array of type integer + StackArray() //sets size of s to 0. + int size() // gets the number of elements in the StackArray (s). + boolean isEmpty() //returns true if s is empty, false otherwise. + void push(int i) //adds integer i at index 0 of the StackArray, the rest of the elements gets pushed each by 1 (i.e. the element at index n gets pushed to index n + 1). As a result, the StackArray increases its size by 1. + int pop() // removes the element at index 0 from the StackArray and returns it. The size of the StackArray should be decremented by 1. + int peek() // returns elements at index 0 without removing it. Class Adder Create a class named Adder that initializes an integer variable named currentValue to 10 (int currentValue = 10;) and then does the following: 1- Prompt the user to enter a number or one of the following three actions: Undo, Redo, Exit 2- If the user entered a number, the number is added to currentValue 3- If the user entered Undo, the last action is undone 4- If the user entered Redo, currentValue takes the value it had just before the last Undo 5- If the user entered Exit, the execution stops. Write appropriate classes or methods to test the operations mentioned above. A sample test file (TestFile.txt) is given to guide your testing. Hint: Declare two StackArray objects, one for the Undo and one for the Redo. 3. Dice In this problem, you are going to perform some experiments that result from rolling two six-sided dice. You will start by creating two classes Die and DicePair that will be used to answer follow-up questions. Die and DicePair are given as follows: Die - rand : Random + Die () + roll() : int DicePair - die1 : Die - die2 : Die + DicePair() + roll() : int The Die class defines a no-arg constructor that creates an instance of the Random class and calls it rand. The class also defines a method roll() that returns a random integer between 1 and 6 inclusive. DicePair objects have two individual dice. The DicePair constructor creates the two Dice instances. The roll() method is then used to simulate the rolling of both dice. It returns the sum of the values returned from rolling each Die. This method should invoke the roll method of the Die instances. Introduction to Programming (CMPS 200) Fall 2011 3 of 4 3.1 Driver Class—DicePairRun Write a driver class (a class that contains a main method) called DicePairRun to roll a pair of dice a certain number of times and store the results in a file. The program accepts two parameters from the command line: an integer n and a file name f. It will start by writing the integer n to f. It will then call the roll() method of the DicePair class n times and store each roll value in f on a separate line each. Note that every time you run the program, you may get different results because of the Random class behavior. Sample run: C:> java DicePairRun 10 dice-pair-runs.txt After running the command above dice-pair-runs.txt contains the following: 10 5 7 3 3 10 9 12 4 8 5 Note: the first line in the file is 10 which is the number of rolls. After the 10, there are 10 lines, each line containing the outcome of one DicePair roll. 3.2 Driver Class—CountOccurrences Write a driver class called CountOccurrences that accepts 2 arguments from the command line: the first is an integer (call it key) between 2 and 12 inclusive and the second is an integer N denoting the number of rolls to perform. Your program should roll the dice pair N times and output the number of occurrences of key in the rolls. Sample run 1: C:> java CountOccurrences 3 6000 [3:304] Sample run 2: C:> java CountOccurrences 12 6000 [12:166] 3.3 Driver Class—FairDie Write a driver class called FairDie that checks the values of n rolls of a single Die and then decides whether the die is fair or not. The program takes an integer N that represents the number of times the die is rolled. If the Die is fair, the program should display on the console “Fair Die”. Otherwise, it will display “Unfair Die”. A Die is considered fair if all six outcomes (1 through 6) are equally likely to occur. The chi-square test is usually used to check whether the die is fair or not. The following formula calculates chi-square: �� � 1 6�� � �6�� � ��� � ��� where, Χ2 = the chi-square value Ci = the number of occurrences of i i.e. the number of occurrences of 1 is C1, the number of occurrences of 2 is C2, the number of occurrences of 3 is C3, etc. n = the number of times the die was rolled Introduction to Programming (CMPS 200) Fall 2011 4 of 4 A Die is considered fair if the value of Χ2 is less than 0.02. Otherwise, it is not fair. Before displaying if the die is fair or not, you should display the value of Chi-Square. Sample run: C:> java FairDie 10000 Chi-Square = 1.2656E-4 Fair Die 4. Courses Write three classes TextBook, Instructor, and Course that aim to organize course offerings. The class called CourseDemo given to you in the end of this problem is used to test your code. The diagram describing the three classes is given below. TextBook -title : String -author : String -publisher : String +TextBook(title : String, author : String, publisher : String) +TextBook(object2 : TextBook) +set(title : String, author : String, publisher : String) : void +toString() : String Instructor -lastName : String -firstName : String -officeNumber : String + Instructor (lname : String, fname : String, office : String) + Instructor (object2 : Instructor) +set(lname : String, fname : String, office : String): void +toString() : String Course -courseName : String -instructor : Instructor -textBook : TextBook +Course(name : String, inst : Instructor, text : TextBook) +getName() : String +getInstructor() : Instructor +getTextBook() : TextBook +toString() : String Question: Explain what problems might occur if the constructor of the Course class does not call the copy constructors for its instructor and textbook instance variables. Draw appropriate diagrams. public class CourseDemo { public static void main(String[] args) { // Create Instructor objects Instructor instructor1 = new Instructor("Turkiyyah", "George", "Bliss 116"); Instructor instructor2 = new Instructor("El-Hajj", "Wassim", "Bliss 104B"); // Create TextBook objects TextBook text1 = new TextBook("Building Java Programs", "Stepp", "Pearson"); // Create Course objects Course course1 = new Course("CMPS 200", instructor1, text1); Course course2 = new Course("CMPS 200”, instructor2, text1); // Display the courses information. System.out.println(course1); System.out.println(course2); }