Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

http://www.megaupload.com/?d=GAGKMH1W There is a link to the picture. Thanks for

ID: 3545314 • Letter: H

Question

http://www.megaupload.com/?d=GAGKMH1W

There is a link to the picture.

Thanks for any help. I really need it.

Create an abstract class named Publication that includes a String field for the Title, a double field for the cost and an integer field for the quantity. With in the Publication class include a constructor that requires all three arguments that will initialize the three data members. Create the get and set methods for the three data members in the class definition. Create an abstract method that will return the price of a book ( the calculation of the price will be different for each subclass). Create a Book subclass that will calculate the price of a book as follows; if the quantity of books is greater than 50 then the price is the cost times 140% minus 5 dollars discount. If the quantity is 50 or below then the 5 dollar discount does not apply. Create a Magazine subclass that has its own data member representing the month of the issue. The constructor for this class should initialize the month data member value. The price for the magazine will be the cost times 160%. You will also need to include get and set methods for the month data member. Create a class named PublicationArray where the main method will create an array of 10 Publications that will hold 5 Books and 5 magazines. Do not Prompt for user input, just hard code the data into your program. Write a for loop to display all the data fields for each element of the array.

Explanation / Answer

public class PublicationArray {
    public static void main(String[] args) {
        Publication[] arr = new Publication[10];
        Book b1 = new Book("book1",100,40);
        Book b2 = new Book("book2",200,50);
        Book b3 = new Book("book3",300,60);
        Book b4 = new Book("book4",400,70);
        Book b5 = new Book("book5",500,80);
       
        arr[0]=b1;arr[1]=b2;arr[2]=b3;arr[3]=b4;arr[4]=b5;
       
        Magazine mag1 = new Magazine("mag1", 100, 10, "April");
        Magazine mag2 = new Magazine("mag2", 200, 20, "April");
        Magazine mag3 = new Magazine("mag3", 300, 30, "April");
        Magazine mag4 = new Magazine("mag4", 400, 40, "April");
        Magazine mag5 = new Magazine("mag5", 500, 50, "April");
        arr[5]=mag1;arr[6]=mag2;arr[7]=mag3;arr[8]=mag4;arr[9]=mag5;
       
        for(int i=0;i<arr.length;i++){
            System.out.println("BOOk["+i+"] is Title : "+arr[i].getTitle()+" Price : "+arr[i].getCost()+" Quantity : "+arr[i].getQuantity()+"   Price: "+arr[i].getPrice()+" Class is "+arr[i].getClass());
        }
       
    }
   
}

abstract class Publication{
    String title;
    double cost;
    int quantity;

    public Publication(String title, double cost, int quantity) {
        this.title = title;
        this.cost = cost;
        this.quantity = quantity;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
   
    abstract double getPrice();
}

class Book extends Publication{
   
    public Book(String title,double cost,int quantity){
         super(title,cost,quantity);
    }
  
    @Override
    double getPrice() {
        if(this.getQuantity()>50){
            return ((this.getQuantity() * this.getCost())*(160/100)-5);
        }else{
            return (this.getQuantity() * this.getCost())*(160/100);
        }
    }

}

class Magazine extends Publication{

     String month;

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }
    
     public Magazine(String title,double cost,int quantity,String month){     
         super(title,cost,quantity);
         this.month = month;
    }
  
    @Override
    double getPrice() {
        return ((this.getQuantity() * this.getCost())*(160/100));
    }
   
}