Author Topic: Java Help  (Read 5101 times)

0 Members and 1 Guest are viewing this topic.

Hey guys,
So I'm stuck in a basic Computer Science class with a professor who might quite possibly be the worst teacher in the history of humankind.
The objective of the class is to learn basic Java, and along with this overall horrendous quality of the teacher, I am pretty horrible when it comes to programming. So if anyone would like to help me out on some problems sometimes that would be absolutely amazing.

For Example, the first Lab Problem this week is:
1) "Design and Implement a class called "Box" that contains instance data (all doubles) that represents the height, width, and depth of the box. Also include a boolean variable called "full" as instance data that represents if the box is full or not. Define the "Box" constructor to accept and initialize the height, width, and depth of the box. Each newly created "Box" is empty (the constructor should initialize "full" to "false".) Include getter and setter methods for all instance data. Add methods, "volume" and "surfaceArea" that compute and return the volume and surface area, respectively of the Box. Include a toString method that returns a one-line description of the box (its height, width, and depth and whether it is full). Create a driver class called "BoxTest", whose main method instantiates and updates several "Box" objects.

Any help at all would be appreciated.

 

Offline Polpolion

  • The sizzle, it thinks!
  • 211
    • Minecraft
Here's one class:

Code: [Select]
public class Box{

private double height, width, depth;
private boolean full;

public Box( double h, double w, double d){
height = h;
width = w;
depth = d;
full = false;
}

public boolean hasContents(){
if(full){return true;}
else {return false;}
}

public void fillBox(){ full = true; }
public void emptyBox() { full = false; }

public double getHeight(){return height;}
public double getWidth(){ return width;}
public double getDepth(){ return depth;}

public void setAll(double h, double w, double d){
height = h;
width = w;
depth = d;
}

public void setHeight(double h){height = h;}
public void setWidth(double w){width = w;}
public void setDepth(double d){depth = d;}

public double getVolume(){return (height*width*depth);}
public double getSurface(){return ( 2*(height*width) + 2*(height*depth) + 2*(depth*height) );}  // assumes box has no opening for stuff to go in/out

public String toString(){
if(full) {return ("A full box that is " + height + " by " + width + " by " + depth + ".");}
else   {return ("An empty box that is " + height + " by " + width + " by " + depth + ".");}
}

}

And the other:

Code: [Select]
public class boxTest{

public static void main(String [ ] args){

Box one =    new Box( 1.0, 2.0, 3.0);
Box two =    new Box( 2.0, 3.0, 4.0);
Box three =  new Box( 3.0, 4.0, 5.0);

//fill all boxes with candy, but then eat all of box two's candy

one.fillBox();
two.fillBox();
three.fillBox();

two.emptyBox();

//makes you cry when you realize how much you've eaten

System.out.println("You ate " + two.getVolume() + " gallons of candy!");

//You modify the boxes so they can hold more candy.

one.setAll(5.0, 5.0, 5.0);
two.setAll(5.0, 5.0, 5.0);
three.setAll(5.0, 5.0, 5.0);

//syntax error

sag84htj jvbkdsnsd(fhgdsgd)

//info about boxes

System.out.println(one.toString());
System.out.println(two.toString());
System.out.println(three.toString());

}

« Last Edit: February 24, 2009, 02:32:58 pm by thesizzler »

 

Offline Flipside

  • əp!sd!l£
  • 212
Hmmm.. where to start....

Obviously, I can't teach you how to code in Java, but I'll try to give a few pointers.

Most important thing is to understand the structure of a Class in Java, which is usually

Code: [Select]
public class XXXX    <---- Defines the Class name
{                             <---- Tell the interpreter that the above name class starts here
int x = 0;
String z = null;
double q = 0.0;      <---- defines the FIELDS of the class, variables are generally accessible to an instantiation of said class

public XXXX()              <----- Defines the constructor, this tell the class what its initial state is when created
{                             <----- Start of constructor
x = 3;
z = "Three";
q = 3.0;
}                             <----- End of constructor

public String getZ()   <----- A Method within the class, something that you can call to either find information or alter data.
{
return z;
}                             <------ End of this method
}                             <------ End of this class


With methods be careful to read up on things like return types, I've used string, but void is the most common, since it doesn't return a value, and doesn't need a return statement in the Method.

I'd suggest going through the stuff the Sizzler posted and finding out why it works, that'll make life a lot easier in the future ;)

Edit:

For the second half, I'll give you one hint, you can define a class as a Field so if you had a constructor that contained:

Code: [Select]
XXXX anobject = new XXXX;
String YYYY = XXXX.getZ();

Then what that would do is create a new object called anobject that is an instance of the XXXX class, the second line would state that the String value YYYY is equal to the result of the Method 'getZ' in that particular instance of the XXXX class, i.e. 'Three'.

That's the very very basics of the language, I could start talking about referencing here, but one step at a time ;)
« Last Edit: February 24, 2009, 02:35:25 pm by Flipside »

 
Thanks guys! I'm trying to teach myself, but like I said, I don't have that much on a natural talent with programming and the professor is terrible.

 
Also don't forget variables x, s, q are private  :)
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Flipside

  • əp!sd!l£
  • 212
Yup, forgot to mention that, I always use private fields and access them through methods if needed, something in me tends to squirm at accessing fields directly from another class unless in very specific conditions ;) (Also it means you can increase protection for threading by using synchronized methods).

Strictly speaking, a class should be as self-contained as possible, so rather than fetching variables from one class and then performing functions on them, you should try to, where possible, create Methods within the Class that has those variables that performs the task, and then pass any required values from the calling Class in the Method call.

Anyway, that's probably going a bit beyond what's required at the moment ;)

 
Yup, forgot to mention that, I always use private fields and access them through methods if needed, something in me tends to squirm at accessing fields directly from another class unless in very specific conditions ;) (Also it means you can increase protection for threading by using synchronized methods).

Strictly speaking, a class should be as self-contained as possible, so rather than fetching variables from one class and then performing functions on them, you should try to, where possible, create Methods within the Class that has those variables that performs the task, and then pass any required values from the calling Class in the Method call.

Anyway, that's probably going a bit beyond what's required at the moment ;)

You SHOULD be squirmish if your accessing variables outside the class, unless the other class is very-very connected with it.

Personally I like the property feature, too bad Java doesn't support it.

Code: [Select]
public String Z
{
   get
   {
         return z ;
   }
    set
   {
      Debug.Assert (value != null, "Attempting to set Z to null") ;
      z = value ;
   }
}
 
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Yup, forgot to mention that, I always use private fields and access them through methods if needed, something in me tends to squirm at accessing fields directly from another class unless in very specific conditions ;) (Also it means you can increase protection for threading by using synchronized methods).

It's also very poor encapsulation if you can directly access another method's variables even if you have get and set functions. It encourages lazy programmers to do so and then complain bitterly at you when you change the internals of your own code.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Flipside

  • əp!sd!l£
  • 212
Heh, exactly, my College project uses about 50 different classes, since it makes life a heck of a lot easier when a certain aspect of the program needs to be changed, I don't have to wade through pages of code to find one subroutine because none of the Classes involved are pages long, at yet, among those 50 classes, there is not one Class that accesses another one's variables directly :)