• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Beginner Question: Class Capabilities

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good reply.
If its your assignment, you should try something out first, and then you can always post specific doubts.

Njoy!!
Sid
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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]
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Evan Cristofori
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done The bit about buses sounds easy enough; you have been told what to do already.
 
Lizunga Lopez
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 24
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Evan check this code it's running as your requirement





with regards,

VIVEK
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far.. I have this.....


Vehicle.java




Bus.java






and my prof says i still have errors :/ :s
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ";

}




}
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and did you work out what I missed out from the Building class?
 
Lizunga Lopez
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when you post source code.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You missed that Vehicle should be abstract.
 
Lizunga Lopez
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why abstract she did not telll me anything!!!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should find oiut about abstract classes here in the Java™ Tutorials.

Although I disagree with the Bicycle examples with public fields.
 
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic