Java Homework Help (object oriented programming)

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
I'm new to programming in general, but I am currently enrolled in intro to Java and need some help with my homework! We are working on "An introduction to object-oriented programming". I need help with #1 and #3 in this lab. Any help would be greatly appreciated!

1. Let's cheat! This program is identical to the sample program of the lesson. Your job is to insert a single line of code where indicated to FORCE the value of dice d1 to be the same as the value of dice d2. HINT: Remember cascading (you can code a call anywhere the returned data type makes sense).
Code:
public class Lab17A {
  public static void main(String[] args) {
    Dice d1 = new Dice();
    Dice d2 = new Dice();
    System.out.println("Start: " + d1.getValue() + ", " + d2.getValue());
    do {
      d1.roll();
      d2.roll();

      // YOUR STATEMENT GOES HERE

      System.out.println("Rolled: " + d1.getValue() + ", " + d2.getValue());
    } while(d1.getValue() != d2.getValue());
    System.out.println("Game over");
  }
}
class Dice {
  private int sides;
  private int value;
  public Dice() {
    setSides(6);
    roll();
  }
  public void setSides(int n) {
    if (n >= 2)
      sides = n;
    else
      sides = 6;
  }
  public void setValue(int n) {
    if (n > 0 && n <= sides)
      value = n;
  }
  public int getSides() {
    return sides;
  }
  public int getValue() {
    return value;
  }
  public void roll() {
    value = (((int)(Math.random() * 1000)) &#37; sides) + 1;
  }
}




3. The Java "newbie" that wrote this didn't know about information hiding. The application uses the Dice class of the lesson and is supposed to FORCE the value of the Dice object to be its highest possible value after it is rolled. As written, the program won't compile. See if you can fix it by changing ONLY the code inside the application class.

Code:
public class Lab17C {
  public static void main(String[] args) {
    Dice d = new Dice();
    d.roll();
    d.value = d.sides; 
    System.out.println("Value: " + d.value);
  }
}
class Dice {
  private int sides;
  private int value;
  public Dice() {
    setSides(6);
    roll();
  }
  public void setSides(int n) {
    if (n >= 2)
      sides = n;
    else
      sides = 6;
  }
  public void setValue(int n) {
    if (n > 0 && n <= sides)
      value = n;
  }
  public int getSides() {
    return sides;
  }
  public int getValue() {
    return value;
  }
  public void roll() {
    value = (((int)(Math.random() * 1000)) % sides) + 1;
  }
}
 

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
This is what I came up with for #1, but the instructor sent it back because it wasn't the correct way of doing it. His only comment was: set ___ (get____)

Code:
public class Lab17A {
  public static void main(String[] args) {
    Dice d1 = new Dice();
    Dice d2 = new Dice();
    System.out.println("Start: " + d1.getValue() + ", " + d2.getValue());
    do {
      d1.roll();
      d2.roll();

    d1 = d2; 
    
      System.out.println("Rolled: " + d1.getValue() + ", " + d2.getValue());
    } while(d1.getValue() != d2.getValue());
    System.out.println("Game over");
  }
}
class Dice {
  private int sides;
  private int value;
  public Dice() {
    setSides(6);
    roll();
  }
  public void setSides(int n) {
    if (n >= 2)
      sides = n;
    else
      sides = 6;
  }
  public void setValue(int n) {
    if (n > 0 && n <= sides)
      value = n;
  }
  public int getSides() {
    return sides;
  }
  public int getValue() {
    return value;
  }
  public void roll() {
    value = (((int)(Math.random() * 1000)) &#37; sides) + 1;
  }
}
 

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
And this is what I submitted for #3 (luckily we can submit as many times as we want) and this was his comment: set ____ (get ... sides). His comments gave me an idea of what I should do, but I just can't seem to get it.

Code:
public class Lab17C {
  public static void main(String[] args) {
    Dice d = new Dice();
    d.roll();

    d.setValue(6);
    d.setSides(6);

    System.out.println("Value: " + d.getValue());
  }
}
class Dice {
  private int sides;
  private int value;
  public Dice() {
    setSides(6);
    roll();
  }
  public void setSides(int n) {
    if (n >= 2)
      sides = n;
    else
      sides = 6;
  }
  public void setValue(int n) {
    if (n > 0 && n <= sides)
      value = n;
  }
  public int getSides() {
    return sides;
  }
  public int getValue() {
    return value;
  }
  public void roll() {
    value = (((int)(Math.random() * 1000)) &#37; sides) + 1;
  }
}
 

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
And if anyone is curious here is the Lesson:



Lesson 17
An introduction to object-oriented programming

Object-oriented programming
&#8226; Simplifies program design by enhancing modularity.
&#8226; Simplifies program development by letting programmers concentrate on smaller pieces of code and letting them use existing code.
&#8226; Simplifies program maintenance by isolating where change must occur.
&#8226; Uses "objects" created from "class" definitions.

An object
&#8226; Is an occurrence (instance) of a class.
&#8226; Contains "encapsulated" data (instance variables) and methods that act upon the object's data.
&#8226; Is assigned its own memory space in an area known as the "heap". When the object is no longer needed, its space is released by the garbage collector.
&#8226; Is conceptually a black box that hides its internal workings but provides public methods that let you use it. These methods act like ports on a computer. To use one, all you need to know is what goes in and what comes out.
For example, visualize this Dice object. It has public methods that let you set the number of sides on the dice, set the dice's value, get the number of sides on the dice, get the dice's value, and roll the dice. You can use it WITHOUT knowing HOW it works. Just "connect" to those public methods...
Code:
 public void setSides(int n)
 
 public void setValue(int n)
 
 public int getSides()
 
 public int getValue()
 
 public void roll()

A class
&#8226; Defines what an object of the class will know (its data) and what it will be able to do (its methods).
&#8226; Typically declares its data to be private. This "information hiding" prevents unauthorized access and abuse. The ONLY way to manipulate the data is through calls to the public methods.
&#8226; Is a template or blueprint from which any number of objects can be created (instantiated). Each object will have identical methods but a different set of instance variables. For example, one Dice object might have 6 sides and a value of 4 and another might have 10 sides and a value of 9.
&#8226; Is a programmer-defined data type. The compiler lets you code a class name ANYWHERE a primitive data type might be coded (in variable declarations, method headers, return statements, casts, etc.).

Sample program
This program has two classes. The Lesson17A class is the main application or "tester" class that creates and uses two Dice objects to play a simple game. It loops to "roll" the two dice until they have identical values. The functionality of each Dice object is defined by the Dice class. Line numbers are for reference purposes only.

Code:
public class Lesson17A {
  public static void main(String[] args) {
    Dice d1 = new Dice();
    Dice d2 = new Dice();
    System.out.println("Start: " + d1.getValue() + ", " + d2.getValue());
    do {
      d1.roll();
      d2.roll();
      System.out.println("Rolled: " + d1.getValue() + ", " + d2.getValue());
    } while(d1.getValue() != d2.getValue());
    System.out.println("Game over");
  }
}
class Dice {
  private int sides;
  private int value;
  public Dice() {
    setSides(6);
    roll();
  }
  public void setSides(int n) {
    if (n >= 2)
      sides = n;
    else
      sides = 6;
  }
  public void setValue(int n) {
    if (n > 0 && n <= sides)
      value = n;
  }
  public int getSides() {
    return sides;
  }
  public int getValue() {
    return value;
  }
  public void roll() {
    value = (((int)(Math.random() * 1000)) &#37; sides) + 1;
  }
}

Notes:
1. Lines 3 and 4 create two Dice objects and assign them to the identifiers d1 and d2. The new keyword requests memory space for the object Dice() is a call to the class constructor method to perform object initialization.
2. Line 5 displays the initial value of each dice by calling each object's getValue method.
3. Lines 7 and 8 "roll" the two dice and line 9 displays their new values.
4. Line 10 determines if the loop will continue by comparing the value of each dice. If the values are different, the loop will go on.
5. Lines 14 through 40 define the Dice class. Specifically,
a. Lines 15 and 16 define instance variables for holding the number of sides and the value of the dice. Because they are declared to be private, they will be accessible ONLY within the Dice class.
b. Lines 17 through 20 define the class constructor method. Notice how the method name is EXACTLY the same as the class name and NO return type is specified (constructor methods are special). This constructor sets the number of sides to 6 and gives the dice an initial roll.
c. Lines 21 through 26 define a setSides method which can be called to set the number of sides on the dice. The parameter it receives is checked for validity and stored if it is greater than or equal to 2. Otherwise, 6 is stored.
d. Lines 27 through 30 define a setValue method which can be called to set the value of the dice. The parameter it receives is checked for validity and stored if it is greater than 0 and less than or equal to the number of sides on the dice.
e. Lines 31 through 33 define a getSides method which can be called to get the number of sides on the dice.
f. Lines 34 through 36 define a getValue method which can be called to get the value of the dice.
g. Lines 37 through 39 define a roll method which can be called simulate rolling the dice to obtain a new value. The complicated formula generates a value from 1 to the number of sides on the dice.
 

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
Problem with #1 is that you don't modify the value of the dice object to be the same, if that's any help.

Problem #3 is pretty straight forward too.

I realize what I said isn't much help, but the instructors hints are actually pretty good, and I really can't say much without giving the answer away.
 

Apathetic

Platinum Member
Dec 23, 2002
2,587
6
81
This is probably giving too much away, but since all I'm doing is quoting a sentence from your instructor... For problem #1, you need to figure out how to do the following with one line of Java:

FORCE the value of dice d1 to be the same as the value of dice d2

Dave
 

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
Thanks guys, I really do appreciate your comments. I am still trying to figure it out. Like I said, this is all new to me and obviously doesn't come easy. I've been trying things and plugging things in and can't get it to compile or work correctly. I'm going to work on some finance homework and then give it another go in a little while.
 

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
Code:
  public static void main(String[] args) {
    Dice d1 = new Dice();
    Dice d2 = new Dice();
    System.out.println("Start: " + d1.getValue() + ", " + d2.getValue());
    do {
      d1.roll();
      d2.roll();

      // YOUR STATEMENT GOES HERE

      System.out.println("Rolled: " + d1.getValue() + ", " + d2.getValue());
    } while(d1.getValue() != d2.getValue());
    System.out.println("Game over");
  }

Okay you need d1.getValue() to return the save value as d2.getValue(), since it seems pretty obvious they don't want you to change the line that outputs the values to the console. To do that you need to change the "value" member variable of each of the dice instances. Since it's "private" you can't access it directly, you need to find some other means of accessing them.
 

Jeraden

Platinum Member
Oct 9, 1999
2,518
1
76
#3 you have the right idea, but try doing it in just 1 line. Instead of hard-coding setting the values/sides to 6, think about how you can just set the value to the number of sides it has? (so it would work whether the dice has 6 sides or 20 sides).
 

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
thanks again guys for the help.

For #1 I got: d1.setValue(d2.getValue());
that seems to work.

and #3: d.setValue(d.getSides());
 

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
The lessons were created by a professor at the school before him, but he updates them and fixes any errors he finds. The book we use is just for reference, but it is:
Introduction to Java Programming Comprehensive Version 8th Edition (Y. Daniel Liang).

If anyone is interested in the entire semester's lessons/labs I can zip them up for you and send them. They are just word documents.