• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance and Boolean conditions  RSS feed

 
Luigi Melanson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone take a look at this code and offer some insight as to what I might be doing wrong? I can't figure out how to add the $220 for the wireless card.

Here are the instructions for the problem:
A class Computer has the following:

Fields:

String manufacturer;
float price;
int memory;
int diskSize;

Constructors:

Computer(){}
Computer(String , float , int , int )

Initializes the four fields, respectively.

Method:

float getFinalPrice(float cdBurner, float DVDPlayer)
Inputs prices for additional hardware, cd burner and DVD palyer. Returns the
total cost after adding price, cd burner cost and DVD player cost.

Write a class Laptop that extends the class Computer. Include the following in
the class Laptop:

1. Add two more fields: boolean wirelessCard and float carryingCase.
2. Two constructors: no-arg constructor, and one six-argument constructor to initialize
all the six fields. Make use of super().
3. Override the getFinalPrice() method. In computing the final price, add $220.00,
if the laptop has a wireless card and add the cost of carrying case.

Write a main method to check the Computer class, Laptop class and the methods.
Do not write your own Computer class. You must use the Computer class provided to
you.

Here is my code thusfar:


class ComputerMain
{
public static void main(String args[])
{
Computer computer1 = new Computer("Dell Precision",800,512,80);
System.out.println(computer1.getFinalPrice(200,100));
Laptop myComputer = new Laptop("Lenovo ThinkPad",1100,1024,160,true,220);
System.out.println(myComputer.getFinalPrice (200,100));
}
}
class Laptop extends Computer
{
boolean wirelessCard;
float carryingCase;
Laptop(){}
Laptop(String Man, float Price, int Mem, int DskMB, boolean wc, float cc)
{
super(Man,Price,Mem,DskMB);
wirelessCard = wc;
carryingCase = cc;

}
float getFinalPrice(float cdBurner, float DVDPlayer, float carryingCase)
{

return (cdBurner + DVDPlayer+carryingCase);
}
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

If you're overriding a method (like getFinalPrice), you need to keep the arguments the same. Otherwise, you're overloading instead of overriding.

The variable wirelessCard is a boolean. So within the method, you need to write some logic to check this value, and if it's true, then add $220 to the float value you're returning. Does that help?

PS: Use Code Tags to keep your formatting intact.
[ December 10, 2006: Message edited by: marc weber ]
 
Luigi Melanson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
Welcome to JavaRanch!

If you're overriding a method (like getFinalPrice), you need to keep the arguments the same. Otherwise, you're overloading instead of overriding.

The variable wirelessCard is a boolean. So within the method, you need to write some logic to check this value, and if it's true, then add $220 to the float value you're returning. Does that help?

PS: Use Code Tags to keep your formatting intact.

[ December 10, 2006: Message edited by: marc weber ]


thanks for taking the time to help me out with this.
I think I am on the right track, I have changed the code as follows,
The next issue I have stumbled over is that I can't seem to pick up the price for the carryingCase and add it to the total (which I changed to 99 Dollars in the code).



class ComputerMain
{
public static void main(String args[])
{
Computer computer1 = new Computer("Dell Precision",800,512,80);
System.out.println(computer1.getFinalPrice(200,100));
Laptop myComputer = new Laptop("Lenovo ThinkPad",1100,1024,160,true,99);
System.out.println(myComputer.getFinalPrice (200,100));
}
}
class Laptop extends Computer
{
boolean wirelessCard;
float carryingCase;
Laptop(){}
Laptop(String Manufacturer, float Price, int memory, int diskSize, boolean wc, float cc)
{
super(Manufacturer,Price,memory,diskSize);
wirelessCard = wc;
carryingCase = cc;
if (wirelessCard) price +=220;

}
float getFinalPrice(float cdBurner, float DVDPlayer, float carryingCase)
{

return (cdBurner + DVDPlayer + carryingCase);
}
 
Divya Shastry
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have noticed the following :

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Divya Shastry:
...System.out.println(myComputer.getFinalPrice (200,100));
// In the above statement you are calling the method of the Laptop class
//with 2 arguments, since such method doesn't exist in the sub class, it
// executes the code of the super class method....

It does exist in the subclass, because it's inherited. (I assume it's not private, final, or static in the superclass because the assignment asks that it be overridden.) However, as already pointed out, changing the argument list in the subclass is overloading -- not overriding.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Luigi Melanson:
...I can't seem to pick up the price for the carryingCase and add it to the total...

The variable "carryingCase" is already defined as a float in the class, so you don't need to pass it as a parameter into the method. (In fact, this is what's causing you to overload instead of override the method.) All you need to do is add carryingCase to the price.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!