Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [Java] Abstract and Extend Class [Solved]
New Posts  All Forums:Forum Nav:

[Java] Abstract and Extend Class [Solved]

post #1 of 3
Thread Starter 
I am having problem with my project.
There are couple extended files but I gonna keep it short here.
all file contain the imports and the right heading.

There are:
main file - contain main method, there is a Database variable
Given file. Cant change this.
Dont know what the last two lines do, beside it is used to sort Warning: Spoiler! (Click to show)
Code:
ComparatorChain chain=new ComparatorChain();
Datebase myList=new Database();

myList.add(new Food("Fruit","Apple","Green");
Collections.sort(myList.item);
chain.addComparator(new sortByName());

Database file - the Database has an Item array (might do arraylist if I get the rest of the code running in time.)
Need help with this Warning: Spoiler! (Click to show)
Code:
private Item[] item;
private int count=0;

public Datebase()
{item=new Item[100];}

public void add(Item input){
item[count]=input;count++
}

Item file - the abstract class Warning: Spoiler! (Click to show)
Code:
private String type;

public Item(String inputType)
{type=inputType;}

public void setType(String inputType)
{type=inputType;}

public String readType()
{return type;}

Food file - food class that is an extend of the Item class Warning: Spoiler! (Click to show)
Code:
private String type, name,color;

public Food(String inputType, String inputName,inputColor)
{type=inputType;
name=inputName;
color=inputColor;}

// there is a setName, readName, setColor, readColor here but not written to keep this shorter.

public String toString()
{ return String.format("%s\t%s\t%s",readType(),readName(),readColor());}

Edited by TLCH723 - 6/27/12 at 7:39am
post #2 of 3
Edit:

You are trying to construct an object of the type "Food" in the third line of the class with the main method (first code) by passing 3 parameters to the constructor.
If you care to look at the code for the "Food" class, you'll see that the constructor in that class accepts only 2 parameters. You forgot to include "inputType" as the third argument, I believe. (and you forgot to write "String" before "inputColor" in the constructor parameters too).

I do believe this is the problem you're having. I'm not sure where you're getting the "count" from. Maybe you missed some part of your code or forgot to mention that you're using some type of loop. ?
 
Desktop
(13 items)
 
MacBook Pro 13"
(6 items)
 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
CPUGraphicsRAMHard Drive
Intel i5 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Lion 13.3" @ 1280 x 800 
  hide details  
Reply
 
Desktop
(13 items)
 
MacBook Pro 13"
(6 items)
 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
CPUGraphicsRAMHard Drive
Intel i5 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Lion 13.3" @ 1280 x 800 
  hide details  
Reply
post #3 of 3
Thread Starter 
Quote:
Originally Posted by {Unregistered} View Post

Edit:
You are trying to construct an object of the type "Food" in the third line of the class with the main method (first code) by passing 3 parameters to the constructor.
If you care to look at the code for the "Food" class, you'll see that the constructor in that class accepts only 2 parameters. You forgot to include "inputType" as the third argument, I believe. (and you forgot to write "String" before "inputColor" in the constructor parameters too).
I do believe this is the problem you're having. I'm not sure where you're getting the "count" from. Maybe you missed some part of your code or forgot to mention that you're using some type of loop. ?
I just mistyped here.
I found the problem and solved it. (Have "abstract" typed)

Now I have another problem.
chain.addComparator(new sortBy*());
I have couples of these, one by name, one by color, one by type, etc.
Collections.sort((ArrayList) myList.readItem(), chain);

I have one that is by a parameter that is only in one extended class.
Code:
class sortByML implements Comparator<Item>
{
        public int compare(Item a,Item b)
        {
                return a.readML().compareTo(b.readML());
        }
}
readML is only in the extended class of Drink.
atm, it doesnt compile since in Item class, it doesnt have readML().
If I change the Item to Drink, I get an error of cannot cast from one type to Drink.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [Java] Abstract and Extend Class [Solved]