aspose file tools*
The moose likes Beginning Java and the fly likes Beginner Question: Class Capabilities Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginner Question: Class Capabilities" Watch "Beginner Question: Class Capabilities" New topic
Author

Beginner Question: Class Capabilities

Evan Cristofori
Greenhorn

Joined: Feb 02, 2007
Posts: 18
Hello All,
I missed a few lectures in class and I got very behind. My assignment is to:
Create a base class named "Rectangle" that contains length and width data members. From this class, derive a class named "Box" having an additional data memner named "depth". The method members of the base "Rectangle" class should consist of a constructor and an override method named "area()" method. The derived "Box" class should have a constructor and an override method named "area()" that retuns the surface area od the box and a "volume()" method.

Include in the classes constructed for the previous exercise in a working Java program. Have yoru program call all of the member methods in each class and explain the result when the area method is called using the two "box" objects.

Can someone guide me in the right direction? Can you show me how to set up the first part? I am very thankful for any help I get.
Srikanth Ramu
Ranch Hand

Joined: Feb 20, 2007
Posts: 76
Let me simplify your requirement. Basically you should have the following:

* One Super class called Rectangle with Length and Rectangle as variables with proper datatype - int, float etc
* Have a method area
------------
* Another class Box should extend Rectangle with a variable depth
* Have a method area applying the Surface Area of a box
* Have another method volume applying the volume formula
----
* Have another Class calling area() method of both Rectangle and Box classess.

Hope this helps
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
Good reply.
If its your assignment, you should try something out first, and then you can always post specific doubts.

Njoy!!
Sid
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
[Nasty pedantic mode]"Base" and "derive" are not Java terms; they are what one says in C#. He means "superclass" and "extend" or "create a subclass of."

[Even more pedantic mode]I hope you never extend Rectangle to create Box when you have finished this course, because Rectangle and Box cannot have an "is-a" relationship with each other. A better inheritance hierarchy would be[/Any pedantry]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3456
    
  12
Originally posted by Campbell Ritchie:
I hope you never extend Rectangle to create Box when you have finished this course, because Rectangle and Box cannot have an "is-a" relationship with each other.


Debatable.
A Box could be a Rectangle with a depth, although obviously RectangularBox would be a better name because you could have a Box that extends Circle (maybe call it HatBox).

I guess it all depends on the system you're designing as to what "is-a" relationships are valid.


Joanne
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Overriding the concrete method area() to give different results will violate LSP, and that's enough to keep some folks from extending Rectangle to make Box.

But you gotta do the homework some times. So, Evan, what do you have so far?
[ April 26, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Evan Cristofori
Greenhorn

Joined: Feb 02, 2007
Posts: 18
Sorry for the delay. I went out of town. This is what I have so far...


On my list to finish is the following...

Box subclass
- extend Rectangle,
- define a private (or possibly protected) variable d (depth),
- add a constructor taking width, height and depth,
- override (replace) the area method of Box and instead supply the Box surface area,
- add a volume method.

Am I on the right track?
Evan Cristofori
Greenhorn

Joined: Feb 02, 2007
Posts: 18
I keep getting an error with this:



symbol : constructor Rectangle()
location: class Rectangle
public Box(int width, int height, int depth) {
^
1 error

Process completed.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Try taking the Box class into its own file and giving it public access. You need to call the superclass constructor; your constructor ought to read
[ April 29, 2007: Message edited by: Campbell Ritchie ]
Evan Cristofori
Greenhorn

Joined: Feb 02, 2007
Posts: 18
Thank you. I had just found that out and was comming back here to to post that I didn't need help anymore. But, thank you very much. I am going to continue the program and will post back if I need help.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Great!

Hey, do the homework as requested, get a good grade and all, but then look up the Liskov Substitution Principle and see if you can spot how extending Rectangle to Box could break it. Was the design in the assignment a good idea? Consider it extra credit work for the ranch. We're generous with pats on the back.
Lizunga Lopez
Greenhorn

Joined: Jun 02, 2011
Posts: 11
Hi i took this class many years ago and reviewing i saw this topic can someone show me the main program or this one completely cause i have some questions with the variables of box thanxs
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Welcome to the Ranch

There isn't a main "program" here; the question was all about inheritance and how you make a class. Please tell us your question.
Lizunga Lopez
Greenhorn

Joined: Jun 02, 2011
Posts: 11
Thanxs a lot I finally did it i made a main program to see if the class was functioning good still gives me 0 the output







that is my main proigram and the 2 classes i am programming in eclipse so if you can help me im running it manually to see the problem why is giving me cero
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
You are getting confused between a public field "area" which is very bad design, and a public method called "area" which is good design. The formula or a Box is incorrect, however.

Get rid of those area fields, which you never use, and replace .area with .area(). That should get rid of the 0.
Lizunga Lopez
Greenhorn

Joined: Jun 02, 2011
Posts: 11
Ohh perfect that was the solution I put the parenthesis in rect1.area();
That worked thanxs very much i will need help with the actually project
I am doing that says:



Create a base class named Vehicle with instance variables make (such as Ford, GM, Toyota, etc.), year , horsepower (200, 300, 400) and milesPerGal. Add the necessary constructor and set and get methods. Then create classes Bus and Truck.
• Bus has an instance variable called numOfPassengers (1 to 50) and Truck has an instance variable called towingCapacity (0.5, 1.0, 2.0, 3.0 tons).
• Both Bus and Truck inherit from Vehicle and have a method called calculateMPG.
o The formula for a bus’s milesPerGal is 10000/numOfPassengers/horsepower, but for a truck it is 5000/towingCapacity/horsepower.
• Create a program that displays all information about a specific bus and truck of your choice.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Well done The bit about buses sounds easy enough; you have been told what to do already.
Lizunga Lopez
Greenhorn

Joined: Jun 02, 2011
Posts: 11
OMG i am having troubles with the class please help...


Create a base class named Vehicle with instance variables make (such as Ford, GM, Toyota, etc.), year , horsepower (200, 300, 400) and milesPerGal. Add the necessary constructor and set and get methods.
vivek kumar jaiswal
Greenhorn

Joined: Jul 31, 2008
Posts: 24

Hi Evan check this code it's running as your requirement





with regards,

VIVEK
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

That area calculation is wrong. It calculates the volume, not the area. The area is the sum of the areas of its 6 sides - two sides with area length * width, two sides with area width * depth, and two sides with area length * depth.

Also, I think it's a bad design to make Box extend Rectangle. It might work in code, but logically it's just wrong - a box is not a rectangle in real life, but in this data model it is. I know that's the class requirement, but it's still just wrong.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3456
    
  12
vivek kumar jaiswal wrote:Hi Evan check this code it's running as your requirement
Evan has probably moved on to other problems in the four years since he posted this question
Lizunga Lopez
Greenhorn

Joined: Jun 02, 2011
Posts: 11
But you can still help me with this exercise!!!

Create a base class named Vehicle with instance variables make (such as Ford, GM, Toyota, etc.), year , horsepower (200, 300, 400) and milesPerGal. Add the necessary constructor and set and get methods. Then create classes Bus and Truck.
• Bus has an instance variable called numOfPassengers (1 to 50) and Truck has an instance variable called towingCapacity (0.5, 1.0, 2.0, 3.0 tons).
• Both Bus and Truck inherit from Vehicle and have a method called calculateMPG.
o The formula for a bus’s milesPerGal is 10000/numOfPassengers/horsepower, but for a truck it is 5000/towingCapacity/horsepower.
• Create a program that displays all information about a specific bus and truck of your choice.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3456
    
  12
Lizunga Lopez wrote:But you can still help me with this exercise!!!
Not until you Show Some Effort we can't

Edit: Actually I've just read the contents of that link and it wasn't what I thought it was. This and this are more the message I was trying to convey.
Lizunga Lopez
Greenhorn

Joined: Jun 02, 2011
Posts: 11
So far.. I have this.....


Vehicle.java




Bus.java






and my prof says i still have errors :/ :s
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Lizunga Lopez wrote: . . . and my prof says i still have errors :/ :s
So you have. You need to consider your class design. Get a pencil and paper and draw the inheritance hierarchy. Also draw a Vehicle. Make sure you know what it looks like.

Then consider the names of your fields. Should those fields be mutable or final? Should yo uhave those set methods? Should you have any non-private fields?

That should give you a few things to be getting on with.
Lizunga Lopez
Greenhorn

Joined: Jun 02, 2011
Posts: 11
Ok let's se if I understood!!! this is what I have done...

vehicle.java

Bus.java


truck.java

I really dont understand very good but can you clarify me? i use the get and set when i will change the variable internally right like mpg??
do I need any final variable here why? or about the non private fields i dont understand that!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
What did your drawing of a Vehicle look like? What does that tell you about the Vehicle class?
All your fields should be private, and some will never change. . . . and I have missed something important out of the Building class.
Lizunga Lopez
Greenhorn

Joined: Jun 02, 2011
Posts: 11
ok finally finish thanxs a lot with tata lab haha

now i have another Lab!!

Design a java interface called Lockable that includes the following methods: setKey, lock, unlock, and locked.
The setKey, lock, and unlock methods take an integer parameter that represents the key.
The setKey method establishes the key. The lock and unlock methods lock and unlock the object, but only if the key passed in is correct. The locked method returns a Boolean that indicates whether or not the object is locked. A Lockable object represents an object whose regular methods are protected: if the object is locked the method cannot be invoked; if it is unlocked, they can be invoked.


so far i have this....

lockable.java

public interface Lockable {
public void setKey(int newkey);
public void lock(int key);
public void unlock(int key);
public boolean locked();

}


but in the class
door.java
public class Door implements Lockable{
private int k;
private boolean nlocked;

public Door(){
k = 0;
nlocked= false;
}

public void setKey(int newkey) {
k = newkey;

}
public void lock(int key) {
if(k == key )
nlocked = true;
}
public void unlock(int key) {
if (k == key)
nlocked = false;
}
public boolean locked() {
return nlocked;
}
public void Hello(){
if(!nlocked)
System.out.println("Hola ");
}
}

but i am not sure if I need another if statement for the methods unlock and lock
Lizunga Lopez
Greenhorn

Joined: Jun 02, 2011
Posts: 11
If a put the key correct or if a put it wrong it keep saying me locked what do I have wrong??



public class Door implements Lockable{
private int k, penelope;
private boolean locked;

public Door(int key){
k = key;
locked= false;// puerta cerrada
}//constructor******

public void setKey(int pepa) {
pepa = 2744;
penelope = pepa;
}// The setKey method establishes the key. 1

public void lock(int key) {
if(k == penelope)// si esta abierta y la llave es la crrecta
locked = true; // cierra
}//
public void unlock(int key) {
if ( k == penelope)
locked = false;

}//The lock and unlock methods lock and unlock the object, but only if the key passed in is correct.
public boolean locked() {
return locked;
}//The locked method returns a Boolean that indicates whether or not the object is locked.
public String toString(){
if(locked = true)return "Locked";
else
return "Unlocked ";

}




}
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Campbell Ritchie wrote:[Nasty pedantic mode]"Base" and "derive" are not Java terms; they are what one says in C#. He means "superclass" and "extend" or "create a subclass of."

I know, I know. The Java class that I taught last term had been taught many times before, so the Department gave me pre-built Powerpoint slides, quiz questions, etc. The material was clearly written for a C++ class, with discussion of "writing a declaration for a bass class" and "write an implementation..."

Lizunga wrote: i am not sure if I need another if statement for the methods unlock and lock

In good design, the need for "lock" and "unlock" methods should be self obvious. Seems to me that your requirements clearly state that you need lock and unlock methods.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
. . . and did you work out what I missed out from the Building class?
Lizunga Lopez
Greenhorn

Joined: Jun 02, 2011
Posts: 11
i dont know what you missed in your Building class my vehicle class with the set and get and set of the milespergalon, the constructor and the tostring method....
did i missed something that worked .... i think cause the prof did not tell me to change aything... :/
vehicle.java
public class Vehicle {

protected String m;
protected int y, hp;
public double mpg;

public Vehicle(String make, int year, int horsepower){ //constructor
m = make;
y = year;
hp = horsepower;
}

public void setmpg(double Fmpg)
{
mpg = Fmpg;
}
public double getmpg()
{
return mpg;
}
public String toString(){
String state = "";
state +="make: " + m;
return state;
}
}
bus.java

public class Bus extends Vehicle{

private int np;

public Bus(String make, int year, int horsepower, int NumPass)
{
super(make, year, horsepower);
np = NumPass;
}
public int getnop(){
return np;
}
public double cMPG (){
mpg = 10000/np/hp;
return mpg;
}
}
truck.javapublic class Truck extends Vehicle{

private double tc;

public Truck(String make, int year, int horsepower, double towingcap) {
super(make, year, horsepower);

tc = towingcap;
}

public double gettc(){
return tc;
}
public double cMPG (){
mpg = 5000/tc/hp;
return mpg;
}
}



Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Please UseCodeTags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
You missed that Vehicle should be abstract.
Lizunga Lopez
Greenhorn

Joined: Jun 02, 2011
Posts: 11
why abstract she did not telll me anything!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
You should find oiut about abstract classes here in the Java™ Tutorials.

Although I disagree with the Bicycle examples with public fields.
 
 
subject: Beginner Question: Class Capabilities