Click to See Complete Forum and Search --> : Constructor


robnyr2317
10-21-2007, 04:46 PM
Im new to java and I need to make a constructor definition that has two parameters, a manufacturers brand and a screen size, the parameters have to bring in information. inside the constructor i have to assign the values taken in from the parameters to the corresponding fields. Here is what I have so far for the constructor but im not sure if it is even the right thing to start with

public class Television
{

private String MANUFACTURER;
private int SCREEN_SIZE;
boolean powerOn;
int channel ;
int volume;

public Television(String MANUFACTURER, int SCREEN_SIZE)
{


}

Here is the other file that is supposed to correspond with the constructor I am trying to make. Thanks for any help.

import java.util.Scanner;

public class TelevisionDemo
{
public static void main(String[] args)
{
//create a Scanner object to read from the keyboard
Scanner keyboard = new Scanner (System.in);

//declare variables
int station; //the user's channel choice

//declare and instatiate a television object
Television bigScreen = new Television("Toshiba" , 55);
//turn the power on
bigScreen.power();
//display the state of the television
System.out.println("A " + bigScreen.getScreenSize() +
bigScreen.getManufacturer() +" has been turned on.");
//prompt the user for input and store into station

System.out.println("What channel do you want? ");
station = keyboard.nextInt();

//change the channel on the television
bigScreen.setChannel(station);
//display the current channel and volume of the television
System.out.println("Channel: " + bigScreen.getChannel() +
" Volume: " + bigScreen.getVolume());
System.out.println("Too loud!! I am lowering the volume.");
//decrease the volume of the television
bigScreen.decreaseVolume();
bigScreen.decreaseVolume();
bigScreen.decreaseVolume();
bigScreen.decreaseVolume();
bigScreen.decreaseVolume();
bigScreen.decreaseVolume();
//display the current channel and volume of the television
System.out.println("Channel: " + bigScreen.getChannel() +
" Volume: " + bigScreen.getVolume());
System.out.println(); //for a blank line


}
}

robnyr2317
10-21-2007, 05:25 PM
This is what i have to do if anyone can help
www.shsu.edu/~csc_tjm/cs146/Lab2.doc

james_
10-24-2007, 03:46 PM
You need to actually set the variables inside the constructor.

Not to give it away exactly, but you want to do something like this:

public Television(String mf, int ss) {
<your field here> = mf;
<your field here> = ss;
}


The way you're calling your constructor is right btw.

Hope this helps, but doesn't give it away entirely.

aksnathan
10-26-2007, 12:19 AM
you have initialize the class members inside the constructor as below


public Television(String MANUFACTURER, int SCREEN_SIZE)
{

this.MANUFACTURER=MANUFACTURER;
this.SCREEN_SIZE=SCREEN_SIZE;

}

aksnathan
10-26-2007, 12:21 AM
you have initialize the class members inside the constructor as below


public Television(String MANUFACTURER, int SCREEN_SIZE)
{

this.MANUFACTURER=MANUFACTURER;
this.SCREEN_SIZE=SCREEN_SIZE;

}