Download 44-141 Exercises 12

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
no text concepts found
Transcript
44-141 Exercises 12
1. Write a driver class named SquareDriver which contains a complete main method and
uses the Square class from Exercise 11. The main method should accomplish the following:






Create an instance of the Square class, named square1, using the Square constructor
with no parameters.
Create an instance of the Square class, named square2, using the Square constructor
with one parameter. The argument passed to constructor should be 5 for the
sideLengthInit.
Using a method from the Square class, set the side length to 10 for the square1 object
reference, using an appropriate method from the Square class.
Using a method from the Square class, print to the console (terminal) window, the
square1 side length. Include an output label which labels the output.
Using a method from the Square class, print to the console (terminal) window, the
square1 area. Include an output label which labels the output
Using a method from the Square class, print to the console (terminal) window, the
sqaure2 perimeter. Include an output label which labels the output.
public class SquareDriver {
public static void testSquare {
Square square1 = new Square();
Square square2 = new Square(5);
square1.setSideLength(10);
System.out.println("Side length of square 1: " +
square1.getSideLength());
System.out.println("Area of square 1: " +
square1.area());
System.out.println("Perimeter of square 2: " +
square2.perimeter());
}
}
2 points for default Square
2 points for Square with side of 5
2 points for changing square1’s size
2 points per “output” line (6 points total)
(12 points for the problem)
2. Write a driver class named RectangleDriver which contains a complete main method
and uses the Rectangle class from Exercise 11. The main method should accomplish the
following:






Create an instance of the Rectangle class, named rectangle1, using the Rectangle
constructor with no parameters.
Create an instance of the Rectangle class, named rectangle2, using the Rectangle
constructor with two parameters. The arguments passed to constructor should be 5 for the
theWidth and 4 for theHeight.
Using a method from the Rectangle class, print to the console (terminal) window, the
rectangle1 width. Include an output label which labels the output.
Using a method from the Rectangle class, print to the console (terminal) window, the
rectangle1 length. Include an output label which labels the output.
Using a method from the Rectangle class, print to the console (terminal) window, the
rectangle2 area. Include an output label which labels the output
Using a method from the Rectangle class, print to the console (terminal) window, the
rectangle2 perimeter. Include an output label which labels the output.
public class RectangleDriver
{
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle();
Rectangle rectangle2 = new Rectangle(5, 4);
System.out.println("Width of rectangle 1: "+
rectangle1.getWidth());
System.out.println("Height of rectangle 1: " +
rectangle1.getHeight());
System.out.println("Area of rectangle 2: " +
rectangle2.area());
System.out.println("Perimeter of rectangle 2: " +
rectangle2.perimeter());
}
}
2 points for default Rectangle
2 points for Rectangle with size of (5,4)
2 points per “output” line (6 points total)
(10 points for the problem)
3. Write a driver class named CircleDriver which contains a complete main method and
uses the Circle class from Exercise 11. The main method should accomplish the following:





Create an instance of the Circle class, named circle1, using the Circle constructor
with no parameters.
Create an instance of the Circle class, named circle2, using the Circle constructor
with one parameter. The arguments passed to constructor should be 5 for theRadius.
Using a method from the Circle class, print to the console (terminal) window, the
circle1 radius. Include an output label which labels the output.
Using a method from the Circle class, print to the console (terminal) window, the
circle2 area. Include an output label which labels the output
Using a method from the Circle class, print to the console (terminal) window, the
circle2 circumference. Include an output label which labels the output.
public class CircleDriver
{
public static void main(String[] args) {
Circle circle1 = new Circle();
Circle circle2 = new Circle(5);
System.out.println("Radius of circle 1: " +
circle1.getRadius());
System.out.println("Area of circle 2: " +
circle2.area());
System.out.println("Circumference of circle 2: " +
circle2.circumference());
}
}
2 points for default Circle
2 points for Circle with radius of 5
2 points per “output” line (6 points total)
(10 points for the problem)
4. Write a Java program that reads names, one per line, from the text file "people.txt". The
program prints the name of each person in the terminal window. For example, if the contents of
the input file is
Bilbo Baggins
Frodo Baggins
Harry Potter
Hermione Granger
then the following names must be displayed in the terminal window:
Bilbo Baggins
Frodo Baggins
Harry Potter
Hermione Granger
import java.io.*;
import java.util.Scanner;
public class People
{
public static void readNames() {
Scanner reader = null
try {
reader = new Scanner(new File("people.txt"));
} catch(FileNotFoundException e) {
System.out.println(“File Not Found!”);
return;
}
while(reader.hasNext()) {
System.out.println(reader.nextLine());
}
reader.close();
}
}
1 point for a “File” object that uses “people.txt”
1 point for try/catch (or specifying that the method throws the exception)
1 point for Constructing a Scanner with the File object
1 point for while-loop that uses either hasNext() or hasNextLine()
1 point for reading from the file via either next()s or nextLine().
1 point for System.out to print the read info.
1 point for closing the file.
(7 points total)
5. Write a Java program that reads exam scores, one per line, from the text file "examscores.txt".
The program computes and prints the average of the exam scores in the file.
import java.io.*;
import java.util.Scanner;
public class Average
{
public static void averageScore()
double total = 0.0;
double average = 0.0;
int count = 0;
Scanner reader = null;
try {
reader = new Scanner(new File("examscores.txt"));
} catch(FileNotFoundException e) {
System.out.println(“File Not Found!”);
return;
}
while (reader.hasNextDouble()) {
total += reader.nextDouble();
count++;
}
average = total / count;
System.out.println(average);
reader.close();
}
}
1 point for a “File” object that uses “examscores.txt”
1 point for try/catch (or specifying that the method throws the exception)
1 point for Constructing a Scanner with the File object
1 point for while-loop that uses either hasNext() or hasNextDouble() or hasNextInt()
1 point for reading from the file via either nextDouble() or nextInt().
2 point for a “total” variable that sums the numbers
2 points for a counter that counts the number of items
1 point for computing the average
1 point for avoiding integer division (either via double variables or typecasting/etc.)
1 point for closing the file.
Note if they use an “int” that a double may be a better choice.
(12 points total)
6. Write a Java program that reads allows the user to type in exam scores until the sentinel value
of -1 is entered. The exam scores should be saved to a file named “examscores.txt”.
import java.util.*;
import java.io.*;
public class ExamScore
{
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
PrintWriter writer = null;
try {
writer = new PrintWriter(new File("examscores.txt"));
} catch(FileNotFoundException e) {
System.out.println(“File Not Found!”);
return;
}
System.out.println("Input a value: ");
double inputValue = reader.nextDouble();
while (inputValue != -1) {
pw.println(inputValue);
System.out.println("Input a value: ");
inputValue = reader.nextDouble();
}
pw.close();
}
}
1 point for a “File” object that uses “examscores.txt”
1 point for try/catch (or specifying that the method throws the exception)
1 point for Constructing a Scanner with System.in
1 point for constructing a PrintWriter using the file object
2 points for a loop using -1 as a sentinel value
1 points for a loop structure that AVOIDS writing the -1 to the file
1 point for closing the file
(8 points total)