7 Replies - 932 Views - Last Post: 09 October 2011 - 09:15 PM Rate Topic: -----

#1 kqmoney31  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 08-May 11

Abstract Class question

Posted 09 October 2011 - 12:41 PM

Hi guys. Basically this is what I'm trying to do:

Create a pricing system for a computer company that sells personal computers over the Internet. There are two kinds of computers, laptops and desktops. Each computer has a processor (1.8. 2.0, 2.5, or 3.0 gigahertz), memory (2, 3 or 4 Gb), hard drive (160, 250, or 500 Gb), and optical drive ("CD-RW", "DVD" or "Blu-ray Combo Drive"). Laptops have 14-, 15- or 17-inch screens. Desktops have 17-, 19-, or 24-inch monitors.

You should have an abstract class Computer and final subclasses LapTop and DeskTop. The 'Computer' class should have set and get methods to modify and retrieve the values of its instance variables (processorSpeed, memorySize, hardDriveSize, and opticalDriveType, and both the 'LapTop' and 'DeskTop' classes should have sets and gets for its only instance variable, the displaySize field. Include validation coding in all the set methods based upon the field values given in the previous paragraph. Since all computers must have all components, establish a default value for each component, i.e. zero (0) or an empty String, meaning that no valid value currently exists for that component. The constructor methods of all three classes should take parameters for initializing all instance variables; additionally the subclass constructors should pass related parameters to the superclass constructor.

Each subclass should have a public method named getComputerPrice(), that calculates and returns a double which is the price of a computer given the base price (different for a laptop or a desktop) plus the cost of the different options (i.e. Price = Base price + Processor price + Memory price + Hard drive price + Optical drive price + Display price). Create an abstract method in the 'Computer' class requiring that this get method be implemented in the two subclasses. Additionally in the 'Computer' class declare abstract prototypes for the setDisplaySize() and getDisplaySize() methods for the display size attribute. (The abstract definitions for the two get methods effectively gives the superclass 'Computer' access to the methods in its subclasses).

Write a single toString() method situated in the 'Computer' class that returns the specifications for each computer object (processor speed, memory size, hard drive size, optical drive type, and display size) with appropriate labeling for readability, plus the computer price which is returned from the getComputerPrice() method of either the LapTop or Desktop subclass (implements late binding).

Declare constants in an interface file named ComputerPricing for the pricing of each of these options as well as the base prices for laptops and desktops Pricing is the same for both laptop and desktop computer components (for example a 1.8 gigahertz processor is the same price whether it is a for laptop or a desktop). The constants for the components may be defined using either of the following:

1. A single constant for every one of the individual component elements, in addition to the base prices for 'Laptop' and 'Desktop' (requires a total of 21 constants), i.e. constants for PROCESSOR_PRICE_1_8, PROCESSOR_PRICE_2_0, and so forth, and including LAPTOP_BASE_PRICE and DESKTOP_BASE_PRICE; using this approach will require a series of If ... Else If statements in the getComputerPrice() method to retrieve the price of each component from the interface and calculate the total computer price.

2. Parallel arrays for each of the components that store all values for the component, i.e. double[] PROCESSOR_SPEED and int[] PROCESSOR_PRICE for the processor; there should be two separate parallel array sets for 'Laptop' and 'Desktop' display prices; this approach will allow for a series of array searches in the getComputerPrice() methods to retrieve the price of each component from the interface and calculate the total computer price; as an additional benefit if you implement this arrays approach, you can use the arrays for the component size and speed values in the set methods thereby eliminating any hardcoded data in the abstract super and final sub classes.

Write a test (driver) class that instantiates objects from the 'LapTop' and 'DeskTop' classes, and which directly or indirectly demonstrates all the set, get and toString() methods of all classes. This test application should instantiate multiple objects of both subclasses, some with valid and some with invalid argument values passed to the constructors. For 10% extra credit, implement downcasting in the driver class by instantiating an array of 'Computer' objects and displaying them as a collection.

This is what i've achieved so far in the Computer abstract class:

public abstract class Computer extends Object
{
   
    private String processorSpeed;
    private String memorySize;
    private String hardDriveSize;
    private String opticalDriveSize;

    
    public Computer(String processorSpeed, String memorySize, String hardDriveSize, String opticalDriveSize)
    {
        setComputer(processorSpeed, memorySize, hardDriveSize, opticalDriveSize);
    }
    
    public void setComputer(String processorSpeed, String memorySize, 
    String hardDriveSize, String opticalDriveSize)
    {
        setProcessorSpeed(processorSpeed);
        setMemorySize(memorySize);
        setHardDriveSize(hardDriveSize);
        setOpticalDriveSize(opticalDriveSize);
    }
    
    public void setProcessorSpeed(String processorSpeed)
    {
        String[] validProcessorSpeed = {"1.8", "2.0", "2.5", "3.0"};
        
        int index;
        
        for (index = 0;
            index < 4 && ! processorSpeed.equalsIgnoreCase(validProcessorSpeed[index]);
            index ++);
            
        if (index < 8)
        {
            this.processorSpeed = validProcessorSpeed[index];
        }
        else
        {
            JOptionPane.showMessageDialog(null, grade + "not a valid processor speed--set to null string");
            this.processorSpeed = null;
            
            
    }
    
    public void setMemorySize(String memorySize)
    {
        
    }
    
    public void setHardDriveSize(String hardDriveSize)
    {
        
    }
    
    public void setOpticalDriveSize(String opticalDriveSize)
    {
        
    }
}



If you guys have any advice for me at all while I'm working on this project, it would be very helpful. I'll keep you guys updated on my code, throughout the day. Because I will be working on this all day.

Is This A Good Question/Topic? 0
  • +

Replies To: Abstract Class question

#2 kqmoney31  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 08-May 11

Re: Abstract Class question

Posted 09 October 2011 - 03:19 PM

This is what my finished abstract computer class looks like. I'm going keep you guys updated on my code.

import javax.swing.JOptionPane;

public abstract class Computer extends Object
{
   
    private String processorSpeed;
    private String memorySize;
    private String hardDriveSize;
    private String opticalDriveType;

    
    public Computer(String processorSpeed, String memorySize, String hardDriveSize, String opticalDriveType)
    {
        setComputer(processorSpeed, memorySize, hardDriveSize, opticalDriveType);
    }
    
    public void setComputer(String processorSpeed, String memorySize, 
    String hardDriveSize, String opticalDriveType)
    {
        setProcessorSpeed(processorSpeed);
        setMemorySize(memorySize);
        setHardDriveSize(hardDriveSize);
        setOpticalDriveType(opticalDriveType);
    }
    
    public void setProcessorSpeed(String processorSpeed)
    {
        String[] validProcessorSpeed = {"1.8", "2.0", "2.5", "3.0"};
        
        int index;
        
        for (index = 0;
            index < 4 && ! processorSpeed.equalsIgnoreCase(validProcessorSpeed[index]);
            index ++);
            
        if (index < 4)
        {
            this.processorSpeed = validProcessorSpeed[index];
        }
        else
        {
            JOptionPane.showMessageDialog(null, processorSpeed + "not a valid processor speed--set to null string");
            this.processorSpeed = null;
        }
            
            
    }
    
    public void setMemorySize(String memorySize)
    {
        String[] validMemorySize = {"2", "3", "4"};
        
        int index;
        
        for (index = 0;
            index < 3 && ! memorySize.equalsIgnoreCase(validMemorySize[index]);
            index ++);
            
        if (index < 3)
        {
            this.memorySize = validMemorySize[index];
        }
        else
        {
            JOptionPane.showMessageDialog(null, memorySize + "not a valid memory size--set to null string");
            this.memorySize = null;
        }
    }
    
    public void setHardDriveSize(String hardDriveSize)
    {
        String[] validHardDriveSize = {"160", "250", "500"};
        
        int index;
        
        for (index = 0;
            index < 3 && ! hardDriveSize.equalsIgnoreCase(validHardDriveSize[index]);
            index ++);
            
        if (index < 3)
        {
            this.hardDriveSize = validHardDriveSize[index];
        }
        else
        {
            JOptionPane.showMessageDialog(null, hardDriveSize + "not a valid hard drive size--set to null string");
            this.hardDriveSize = null;
        }
    }
    
    public void setOpticalDriveType(String opticalDriveType)
    {
        String[] validOpticalDriveType = {"CD-RW", "DVD", "Blu-ray Combo Drive"};
        
        int index;
        
        for (index = 0;
            index < 3 && ! opticalDriveType.equalsIgnoreCase(validOpticalDriveType[index]);
            index ++);
            
        if (index < 3)
        {
            this.opticalDriveType = validOpticalDriveType[index];
        }
        else
        {
            JOptionPane.showMessageDialog(null, opticalDriveType + "not a valid optical drive type--set to null string");
            this.opticalDriveType = null;
        }
    }
    
   public String getProcessorSpeed()
   {
       return processorSpeed;
   }
   
   public String getMemorySize()
   {
       return memorySize;
    }
    
   public String getHardDriveSize()
   {
       return hardDriveSize;
    }
    
    public String getOpticalDriveType()
    {
        return opticalDriveType;
    }
    
    public String toString()
    {
        return "Processor Speed: " + getProcessorSpeed() + 
                "Memory Size: " + getMemorySize() +
                "Hard Drive Size: " + getHardDriveSize() +
                "Optical Drive Type: " + getOpticalDriveType();
            }
}


Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10466
  • View blog
  • Posts: 38,793
  • Joined: 27-December 08

Re: Abstract Class question

Posted 09 October 2011 - 03:43 PM

You know that you don't have to explicitly extend Object, right? All classes extend Object, even if you do not spell out extends Object.
Was This Post Helpful? 1
  • +
  • -

#4 kqmoney31  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 08-May 11

Re: Abstract Class question

Posted 09 October 2011 - 06:34 PM

Hi guys its me again. I'm actually kind stuck now and confused. I'm working on the Laptop class now and its giving me an error.

public final class Laptop extends Computer
{
    private String displaySize;
    
    public Lapttop(String processorSpeed, String memorySize, String hardDriveSize, String opticalDriveType)
    {
        super(processorSpeed, memorySize, hardDriveSize, opticalDriveType);
        setDisplaySize();
    }
    
    public void setDisplaySize(String displaySize)
    {
        String[] validDisplaySize = {"14", "15", "17"}
        
        int index;
        
        for (index = 0;
            index < 3 && ! displaySize.equalsIgnoreCase(validDisplay[index]);
            index ++);
            
        if (index < 3)
        {
            this.displaySize = validDisplaySize[index];
        }
        else
        {
            JOptionPane.showMessageDialog(null, displaySize + "not a valid display size--set to null string");
            this.displaySize = null;
        }
    
        
    }
    
    public String getDisplaySize()
    {
        return displaysize;
    }
    
 
}


Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10466
  • View blog
  • Posts: 38,793
  • Joined: 27-December 08

Re: Abstract Class question

Posted 09 October 2011 - 08:00 PM

And the error is? Please remember to specifically describe your problems or errors.
Was This Post Helpful? 0
  • +
  • -

#6 kqmoney31  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 08-May 11

Re: Abstract Class question

Posted 09 October 2011 - 08:03 PM

nevermind I fixed it. If i run into any other troubles, ill sure to post them here.
Was This Post Helpful? 0
  • +
  • -

#7 kqmoney31  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 08-May 11

Re: Abstract Class question

Posted 09 October 2011 - 09:06 PM

Alirite guy, i'm officially stuck at this part:

Each subclass should have a public method named getComputerPrice(), that calculates and returns a double which is the price of a computer given the base price (different for a laptop or a desktop) plus the cost of the different options (i.e. Price = Base price + Processor price + Memory price + Hard drive price + Optical drive price + Display price). Create an abstract method in the 'Computer' class requiring that this get method be implemented in the two subclasses. Additionally in the 'Computer' class declare abstract prototypes for the setDisplaySize() and getDisplaySize() methods for the display size attribute. (The abstract definitions for the two get methods effectively gives the superclass 'Computer' access to the methods in its subclasses).

import javax.swing.JOptionPane;

public final class Laptop extends Computer
{
    private String displaySize;
    
    public Laptop(String processorSpeed, String memorySize, String hardDriveSize, String opticalDriveType)
    {
        super(processorSpeed, memorySize, hardDriveSize, opticalDriveType);
        setDisplaySize();
    }
    
    public double getComputerPrice()
    {
        computerPrice = laptopPrice + processorPrice + hardDrivePrice + opticalDrivePrice;
        return computerPrice;
        return laptopPrice;
        return processorPrice;
        return;
    }
    
    public void setDisplaySize()
    {
        String[] validDisplaySize = {"14", "15", "17"};
        
        int index;
        
        for (index = 0;
            index < 3 && ! displaySize.equalsIgnoreCase(validDisplaySize[index]);
            index ++);
            
        if (index < 3)
        {
            this.displaySize = validDisplaySize[index];
        }
        else
        {
            JOptionPane.showMessageDialog(null, displaySize + "not a valid display size--set to null string");
            this.displaySize = null;
        }
    
        
    }
    
    public String getDisplaySize()
    {
        return displaySize;
    }
    
    public String getDisplaySizeString()
    {
        return "Display Size: " + getDisplaySize();
    }
    
 
}


Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10466
  • View blog
  • Posts: 38,793
  • Joined: 27-December 08

Re: Abstract Class question

Posted 09 October 2011 - 09:15 PM

An abstract method is declared with the abstract keyword. It looks like:
public abstract void foo();



Just rather than declaring the method body with curly braces, you end it with a semi-colon. Otherwise, it's just a normal method definition.

public abstract class Test{

    //this is a method
    public void foo(){}

    //this is an abstract method that must be
    //overridden in its concrete subclasses
    public abstract int x();
}

class A extends Test{

     //since A is not abstract, it must
     //implement all the abstract methods
     //from Test
    public int x(){ return 0; }
}



japanir has a good tutorial on Abstract Classes vs. Interfaces you may find helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1