CS2302/5302 Data Structures and Algorithms

CS2312 Problem Solving and Programming [2016-17 Semester B]
Quiz 3
Apply best practices and skills of object-oriented programming.
Pay attention to code design, naming and code formatting.
Program execution speed is NOT a major concern.
Note: Whenever appropriate, use for-each loop.
Duration: 40 minutes. Total mark: 40 marks (Q1: 20 marks; Q2: 20 marks)
Question 1 (20 marks)
The following program models the companies who produce products and the salespersons who sell
them and earn commission points from the companies.
-
Each product is produced by one company only. A salesperson earns 1 point for selling each item.
-
main()is given below. Its outputs are given in the underlined comments.
The Company class is also given below. It is extremely simplified, but is sufficient for this program.
-
Your tasks: (a) Write the classes Product and SalesPerson class (15 marks);
(b) Finish the drawing in the answer sheet (5 marks)
public static void
{
Company a = new
Company b = new
Company c = new
Product
Product
Product
Product
p1
p2
p3
p4
=
=
=
=
main(String[] args)
public class Company
{
public Product produce() {
return new Product(this);
}
}
Company();
Company();
Company();
a.produce();
b.produce();
c.produce();
c.produce();
SalesPerson helena = new SalesPerson();
SalesPerson paul = new SalesPerson();
helena.sell(p1);
helena.sell(p3);
helena.sell(p3);
helena.sell(p4);
//earn one commission point from company a
//earn one commission point from company c
//earn one commission point from company c
//earn one commission point from company c
helena.showEarnFrom(a); //Output:
helena.showEarnFrom(b); //Output:
helena.showEarnFrom(c); //Output:
paul.showEarnFrom(a); //Output: 0
paul.showEarnFrom(b); //Output: 0
paul.showEarnFrom(c); //Output: 0
1 points
0 points
3 points
points
points
points
}
1
Question 2 (20 marks)
We are to analyze the profits earned in 12 months stored a file. For simplicity, only the total is reported,
as shown below:
Test cases:
Case 1: The file profits.txt has 12 numbers:
Expected Outputs: .
113890000
101200000
98040000
97670000
94740000
102990000
114920000
113180000
82950000
119060000
80620000
93560000
Total: 1212820000
Report finished without error.
Case 2: The file profits.txt does not exist.
Cannot open input file!
Case 3: The file profits.txt exists but has less than 12 numbers.
Insufficient data!
Case 4: The file profits.txt has non-integer like “oneMillion”
Wrong data!
Total: [cannot be calculated]
Total: [cannot be calculated]
For simplicity, assume that case 3 and case 4 will not happen at the same time.
(a) Mary has written the program on next page. However, she did not make use of Exception Handling
correctly. What will be the outputs from her program? (6 marks)
(b) Mary’s program needs to be corrected and improved. Rewrite it with proper code. (14 marks)
Note: the program outputs should be the same as the Expected Outputs.
2
Mary’s program:
public static void main(String[] args)
{
try {
String report = analyze();
System.out.println(report);
} catch (FileNotFoundException e) {
System.out.println("Cannot open input file!");
}
System.out.println("Report finished without error.");
}
---------------------------------------------------------------
private static String analyze() throws FileNotFoundException {
int total=0;
Scanner f = new Scanner(new File("profits.txt"));
for (int i=0;i<12;i++)
{
try {
int value = f.nextInt();
total+=value;
//for simplicity, assume no overflow problem
} catch (InputMismatchException e) {
System.out.println("Wrong data!");
total=-1;
} catch (NoSuchElementException e) { //NoSuchElementException happens when it
//
attempts to read after the end of the file
System.out.println("Insufficient data!");
total=-1;
}
}
f.close();
if (total==-1)
return "Total: [cannot be calculated]";
else
return "Total: "+total;
}
3
4
CS2312 Problem Solving and Programming [2016-17 Semester B]
Quiz 3 – Answer paper
Student ID No: ______________
5
Q1 (a) public class Product{
}
import java.util.ArrayList;
public class SalesPerson {
}
6
Student ID No: ______________
Q1 (b) Complete the following drawing to show all variables of main(), except args, and all the objects linked
directly and indirectly from the variables, including array lists and objects linked from the array lists,
which appear when the program runs.
Try your best to illustrate them in your drawing, using arrows, boxes, and writing down the stored
contents in the variables and object fields.
7
Q2. (a) For each case, choose your answer from the choices: A, B, or C. (6 marks)
Test case 1:
The file profits.txt has 12 numbers:
113890000
101200000
98040000
97670000
94740000
102990000
114920000
113180000
82950000
119060000
80620000
93560000
A.
Total: 1212820000
B.
Report finished without error.
C.
Total: 1212820000
Report finished without error.
Your answer: ______
Test case 2:
The file profits.txt does not exist.
A.
Cannot open input file!
Insufficient data!
Report finished without error.
B.
Cannot open input file!
C.
Cannot open input file!
Report finished without error.
Your answer: ______
Test case 3:
The file profits.txt exists but has 10 numbers only.
A.
Insufficient data!
Report finished without error.
B.
Insufficient data!
Total: [cannot be calculated]
Report finished without error
C.
Insufficient data!
Insufficient data!
Total: [cannot be calculated]
Report finished without error
Your answer: ______
8
Q2. (b)
Student ID No: ______________
public static void main(String[] args)
{
try {
String report = analyze();
System.out.println(report);
}
--------------------------------------------------------------private static String analyze() throws FileNotFoundException
{
int total=0;
Scanner f = null;
9
}
Mark: _____ (Out of 40)