Java Constructors?

+2 Claude Mona · October 28, 2015
Alright I'm stuck on 4 & 5 haven't started 6 yet. Not looking for anybody to do them just would like some help on finishing up 4 & 5 and possibly give any feedback on the code so far.
 
2 .Create Instance Variables (attributes) 
        Create at least 3 private instance fields (attributes) for your class
        You must use at least 3 different data types for your fields
3. Create getter (accessor) and setter (mutator) methods 
       Create a getter (accessor) method for each of your instance variables 
       Create a setter (mutator) method for each of your instance variables 
4. Create a Method to display your data 
       Create a method called display, that simply prints out the values of all instance variables of your object
5. Create 2 Constructors
       Create a default constructor (no parameters) that assigns all your instance variables to default values 
       Create a parameterized constructor that takes all instance variables as parameters, and sets the instance variables to the values provided by the parameters 
6. Testing your program 
      Create a class called Demo.java. This class will contain your main method
      Create an instance of your class by using the default constructor. 
      Call all your objects set methods to assign values to your object
      Call the objects display method, to print out it's values
      Create another instance of your class by using the parameterized constructor 
      Call the objects display method, to print out it's values

public class Coffee {

//Instance Variables
private double sugar;
private int milk;
private boolean heat; 

//Constructor
public Coffee (double id, int dairy, boolean temp )
{

sugar = id;
   milk = dairy;
heat = temp;

}


// (setter)

public void setSugar(double id){
sugar = id;
}
public void setMilk(int dairy){
milk = dairy;
}
public void setSize(boolean temp){
heat = temp;
}

//(getter)

public double getSugar(){
return sugar;
}

public int getMilk(){
return milk;
}

public boolean temp(){
return heat;
}

{

}
//Method to display data, (need to work on this)
static void display()
  {

System.out.println("You added +sugar+ tablespoons of sugar to your coffee");
   System.out.println("You have +dairyin your coffee");
   System.out.println("That's a +size+ ounce cup");
 
 }
//Default Constructor (need help with setting heat to a default)
public Coffee() {
   sugar = 0;
   milk = 0;
   heat = 0;
}
  
}


Post a Reply

Replies

Oldest  Newest  Rating
0 bebedoggy dog · April 3, 2016
hi anyone can help me out with some codes???? pls drop me a email at wenhan999@hotmail.com
0 Nicholas Eason · April 3, 2016
As opposed to what Kuroodo said, you don't HAVE to put your constructors as the first method, however it tends to look nicer if you do.

Mario, the purpose of this exercise is to demonstrate object creation, so for this exercise, you have everything done correctly. To display the information, you'd call
shoes.display();
0 mario huerta · April 2, 2016
Hey did you do part 6 yet? I'm doing a similar program and need help with part 6.


public class Shoes {


//Instance Variables
private String brand; // The shoes brand
private String color; // The shoes color
private double size;  // The shoes size


// Assigns instance variables to default values
public Shoes(){
brand = "";
color = "";
size = 0;
}

//Constructor
public Shoes (String brand, String color, double size)
{
this.brand = brand;
this.color = color;
this.size = size;
}


/**
* The setBrand method stores a value in the brand field.
* @param brand The value to store in Brand.
*/

public void setBrand (String brand)
{
this.brand=brand;
}

/**
* The setColor method stores a value in the color field.
* @param color the value to store in color
*/

public void setColor (String color)
{
this.color=color;
}

/**
* The setSize method stores a value in the size field.
* @param size the value to store in size
*/

public void setSize (double size)
{
this.size=size;
}

/**
* The getBrand method returns Shoes brand.
* @return the value in the brand field
*/

public String getBrand()
{
return brand;
}

/**
* the getColor method returns Shoes color.
* @return the value in the color field
*/

public String getColor()
{
return color;
}

/**
* the getSize method returns Shoes size
* @return the value in the size field
*/

public double getSize()
{
return size;
}

// prints out the values of all instance variables of your object
public void display()
{
System.out.println(brand);
System.out.println(color);
System.out.println(size);

}


}

This is my Demo class that I'm stuck with. Am i supposed to get input or put the values myself? Also how do I "Call the objects display method, to print out it's values"? Please any help would be appreciated.


/**
 * This program demonstrates the Shoes class's
 * setBrand, setColor, setSize, getBrand, getColor
 * and getSize methods.
 *
 */

public class Demo {

public static void main(String[] args) {
// Create a object
Shoes shoes = new Shoes();

//Call  the object's setBrand method, passing Nike as a argument.
shoes.setBrand("Nike");

//Call  the object's setColor method, passing Pink as a argument.
shoes.setColor("Pink");

//Call  the object's setSize method, passing 7 as a argument.
shoes.setSize(7);




}

}
0 Milorad Jekic · October 28, 2015
man you are great! i add you for friend :)
+2 Kuroodo Ditory · October 28, 2015
As Milorad mentioned, heat is a boolean and can only be set to either true or false. In some cases, 0 and 1 can be used to represent booleans.

You are done with your constructors, you did that the correct way! You should have all your constructors at the top though. Example:


private double sugar;
private int milk;
private boolean heat;


// Default Constructor (need help with setting heat to a default)
public Coffee() {
sugar = 0;
milk = 0;
heat = false;
}

// Constructor
public Coffee(double id, int dairy, boolean temp) {

sugar = id;
milk = dairy;
heat = temp;

}

public void myOtherMethods(){
}




You also have 2 brackets { } randomly floating about (under the getter method for temp/getting heat, above display.

Your display method does not have to be static. Static stuff is a different thing that I can explain if you would like.
Change your display method to a public void. Then prints out your values.

public void display() {

System.out.println(sugar);
System.out.println(milk);
System.out.println(heat);

}


Text in java is known as a String. Strings are always surrounded by quotes " " . You can combine variables and strings at any time!

You can do things like this:


System.out.println("You have " + sugar + " sugar in your cup");

Let's say sugar is equal to 5.

The output would be: You have 5 sugar in your cup






.

Extra:

Here is another way to set values in a constructor

public Coffee(double sugar, int milk, boolean heat) {

this.sugar = sugar;
this.milk = milk;
this.heat = heat;

}


Basically, the names of the variables in the parameters are the same as the ones in our Coffee class. In order to distinguish between them, we can use "this".

What "this" means is that it will refer to the variables from the class, and not the ones within the method. To further understand what I mean, copy and past the constructor I made. Click on sugar (the one with "this" on it.), you will see that it will be highlighted, as well as the ones all over the class. Now remove the this part (make this.sugar into sugar). Now click on it again. You will see that it only highlights the sugar variable inside of the method.
+1 Milorad Jekic · October 28, 2015
im just a starter, but it seams to me that heat cannot be equal to 0 since u declared heat as boolean which can be true or false.
  • 1

Java / Android Development

107,237 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator