0% found this document useful (0 votes)
23 views3 pages

CSCI300 - Sample Final Exam - Solution

The document outlines a programming assignment involving a DrugStore system with classes for Medicine, OverCounter, and Prescription. It includes test cases for various functionalities and the implementation of methods to manage medicines and calculate prices after tax. The code demonstrates object-oriented principles such as inheritance and interfaces in Java.

Uploaded by

alibadran2001.ab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

CSCI300 - Sample Final Exam - Solution

The document outlines a programming assignment involving a DrugStore system with classes for Medicine, OverCounter, and Prescription. It includes test cases for various functionalities and the implementation of methods to manage medicines and calculate prices after tax. The code demonstrates object-oriented principles such as inheritance and interfaces in Java.

Uploaded by

alibadran2001.ab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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________________________________

public class OverCounter extends Medicine implements Taxable{ //1pt


private boolean isSupplement;

public OverCounter(boolean isSupplement, String name, double price) { //1 pt


super(name, price); //2pts
this.isSupplement = isSupplement; //1pt
}

public boolean isSupplement() {


return isSupplement;
}
public void setIsSupplement(boolean isSupplement) {
this.isSupplement = isSupplement;
}

public String toString() {//5pts


return "OverCounter{" + super.toString() + " isSupplement=" + isSupplement + '}';
}

public double findPriceAfterTax(){//5pts


return price + price * 0.1;
}
}
______________________18 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;

public class Driver {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//a.
DrugStore p = new DrugStore("Zeina", "Hamra"); // 5 pts

//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
}
}
______________________________________________________

public class Prescription extends Medicine{


private String code;

public Prescription(String code, String name, double price) {


super(name, price);
this.code = code;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "Prescription{" + super.toString()+ " code=" + code + '}';
}
}

You might also like