CSCI300 - Sample Final Exam - Solution
CSCI300 - Sample Final Exam - Solution
Part 1
Question1
------------------ Run1--------------
Test1
o
Test4
Test5
Test6
// 1pt each
------------------ Run2--------------
Test1 // 1pt
Test3 // 1pt
Test5 // 2pts
Test6 // 1 pt
Question2
Parent // 2.5pts
Parent // 2.5pts
You pressed Cancel. //2 pts
Cancel //2 pts
You pressed OK. //2 pts
Ok //1 pt
Question3
0 // 1pt
1.5 tsp Baking powder // 1pt
4 // 1pt
Sweet recipe // 1pt
3 // 1pt
1 cup Flour //2 pts
1 cup Milk //2 pts
Sugar // 1pt
Salt // 1pt
1 egg //2 pts
Part 2
________________________5 points_____________________________
public interface Taxable { //2 pts
double findPriceAfterTax(); //3 pts
}
________________________7 points_____________________________
public abstract class Medicine { //1 pt
protected String name; // ½ pt
protected double price; // ½ pt
public Medicine(String name, double price) { // 1 pt
this.name = name; // 1 pt
this.price = price; // 1 pt
}
public String toString() {
return "name=" + name + ", price=" + price; // 2 pts
}
}
______________________15 points________________________________
import java.util.ArrayList;
public class DrugStore {
private String name, location; // 1 pt
private Medicine [ ] medicines; // 1 pt
private int numberOfMedicines; // 1 pt
public DrugStore(String n, String loc, int size) {
name = n; // 1 pt
location = loc; // 1 pt
medicines = new Medicine[size]; // 1 pt
numberOfMedicines = 0;
}
public void addMedicine(Medicine m){
if (numberOfMedicines == medicines.length)
System.out.println("The array is full");
else
medicines[numberOfMedicines++] = m; // 1 pt
}
public double buyMedicine(String name){
for(int i = 0; i < numberOfMedicines; i++){
if(medicines[i].name.equalsIgnoreCase(name))
if(medicines[i] instanceof Prescription)
return medicines[i].price;
else
return ((Taxable) medicines[i]).findPriceAfterTax();
}
return 0; //Medicine not found
}
public OverCounter[] getOverCounterMeds(){// 1 pt
int count = 0; // 1 pt
for(int i = 0; i < numberOfMedicines; i++) // 1 pt
if(medicines[i] instanceof OverCounter) // 1 pt
count++;// 1 pt
OverCounter[] overCounterMeds = new OverCounter[count]; // 1 pt
int index = 0;
for(int i = 0; i < numberOfMedicines; i++){// 1 pt
Medicine m = medicines[i]; // 1 pt
if(m instanceof OverCounter) // 1 pt
overCounterMeds[index++] = (OverCounter)m; // 1 pt
}
return overCounterMeds; // 1 pt
}
}
______________________20 points________________________________
import java.util.Scanner;
//b.
p.addMedicine(new OverCounter(false, "Advil", 10000)); // 3 pts
p.addMedicine(new OverCounter(true, "Zinc", 12000)); // 3 pts
p.addMedicine(new Prescription("ZAXA", "Omnicef", 20000)); // 3 pts
//c.
System.out.println("Number of over the counter medicines is: " +
p.getOverCounterMeds().length); // 6 pts
}
}
______________________________________________________