Download Assignment 8

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Corecursion wikipedia , lookup

Transcript
Programming Assignment 8
Due May11, 2012
Send your source code and a document (only for the program practice problem)
to (with Subject: A8-Sp12 cmp326 Firstname.Lastname)
[email protected]
Program Task 1
In chapter 7, we studied Calculator.java. If the operands are not integer values,
Integer.parseInt(args[0]) or Integer.parseInt(args[2]) throws NumberFormatException. Rewrite
the following program with an exception handler that deals with numeric operands.
public class Calculator {
public static void main(String[] args) {
// Check command-line arguments
if (args.length != 3) {
System.out.println(
"Usage: java Calculator operand1 operator operand2");
System.exit(0);
}
// The result of the operation
int result = 0;
// Determine the operator
switch (args[1].charAt(0)) {
case '+': result = Integer.parseInt(args[0]) +
Integer.parseInt(args[2]);
break;
case '-': result = Integer.parseInt(args[0]) Integer.parseInt(args[2]);
break;
case 'x': result = Integer.parseInt(args[0]) *
Integer.parseInt(args[2]);
break;
case '/': result = Integer.parseInt(args[0]) /
Integer.parseInt(args[2]);
}
// Display result
System.out.println(args[0] + ' ' + args[1] + ' ' + args[2]
+ " = " + result);
}
1
}
Program Task 2
Create a class Rational that represents a rational number. It should have private instance
variables for
• The numerator (an integer)
• The denominator (an integer)
And the following methods:
• Rational(numerator, denominator)—a constructor for a rational number.
• Accessor methods getNumerator and getDenominator and mutator methods
setNumerator and setDenominator for the numerator and the denominator. You should
use an exception to guarantee that the denominator is never zero.
(1) Write Rational.java. setDenominator method should throws ArithmeticException.
(2) Modify the TestRational.java to handle exceptions thrown.
public class TestRational {
public static void main(String[] args) {
Rational x = new Rational(10, 13);
System.out.println("The constructor should give us 10/13");
System.out.println("The rational is " + x + " or " + x.value());
System.out.println("Numerator is " + x.getNumerator());
System.out.println("Denominator is " + x.getDenominator());
System.out.println();
System.out.println("Changing the numerator to -24 ");
x.setNumerator(-24);
System.out.println("The rational is " + x + " or " + x.value());
System.out.println("Numerator is " + x.getNumerator());
System.out.println("Denominator is " + x.getDenominator());
System.out.println();
System.out.println("Changing the denominator to -30 ");
x.setDenominator(-30);
System.out.println("The rational is " + x + " or " + x.value());
System.out.println("Numerator is " + x.getNumerator());
System.out.println("Denominator is " + x.getDenominator());
System.out.println();
System.out.println("Trying to set the denominator to 0 ");
x.setDenominator(0);
System.out.println("The rational is " + x + " or " + x.value());
System.out.println("Numerator is " + x.getNumerator());
System.out.println("Denominator is " + x.getDenominator());
2
System.out.println;
}
}
Program Task 3
Revise the class Pet, as shown in Listing 6.1 of Chapter 6 (refer to the handout), so that it is
serializable. Remove writeOutput() method from the Pet class and add public String
toString() method to show the content of a Pet object to the user.
Program Task 4
Write a program (PetObjectWriteToFile.java) that allows you to create Pet objects and write
them to a file. The user is asked to enter the file name to store objects to be created. Your
application then iteratively gets Pet data from the user to create Pet objects to be stored in the file
(e.g., objectStream.writeObject(aPet)) until the user confirms that there is no more object
to be created.
Program Task 5
Write a program (PetObjectReadFromFile.java) that allows you to read Pet objects from a
file that stores serialized Pet objects. The user is asked to enter the file name to read objects from
the file. Your application then iteratively gets Pet data from the file (e.g.,
(Pet)objectStream.readObject()) until the end of file is encountered (until EOFException
occurs), then prints out the content of Pet objects to the console by calling toString() method of
the Pet class. Add a method to display the name and weight of the heaviest pet, the name and
weight of the lightest pet, the name and age of the youngest pet, and the name and age of the
oldest pet.
Program Task 6



Write a recursive version of Java program that calculates sum of integer values starting
from lower bound to upper bound.
Write a recursive version of Java program that computes the number of odd digits in a
positive integer number.
Write a recursive version of Java program that computes the sum of all the values in an
array.
Program Practice Problem (write a brief report)

Compare merge sort (recursive version) and selection sort (in chapter 7) by measuring
computing time of each program. Use System.nanoTime() method (see below) before and
after performing search. Do the performance evaluation with an array of 100, 1000,
3
10000 integer values. Each integer value (between 0 and 10000 excluding 10000) is
created by Math.random() method.
public static long nanoTime()
Returns the current value of the most precise available system timer, in nanoseconds.
This method can only be used to measure elapsed time and is not related to any other
notion of system or wall-clock time. For example, to measure how long some code takes
to execute:
long startTime = System.nanoTime();
// ... the code being measured ...
long estimatedTime = System.nanoTime() - startTime;

Practice the following 2 programs and draw runtime stack frames for each program.
public class Example1
{
public static void main(String[] args)
{
displaySharps(3);
}
public static void displaySharps(int n)
{
if (n <= 1)
System.out.println('#');
else
{
System.out.print('#');
displaySharps(n - 1);
}
}
}
public class Example2
{
public static void main(String[] args)
{
System.out.println(mysteryValue(3));
}
public static int mysteryValue(int n)
{
if (n <= 1)
return 1;
else
return (mysteryValue(n - 1) + n);
}
}
4