Monday, January 22, 2007

Lab 2

Please Post your ideas for Lab 2 here! I haven't started yet, my labs are on tuesday!

For Lab Instructions: Click

11 comments:

Yuri said...

if someone needs to start and is lost... at least i know why ppl dont go to labs anymore, doesnt help at all, even thou there were only 4 ppl in there.




package lab2;
import java.util.*;
public class Television{
public static final int MAX_SCREENSIZE = 50;
public static final int MIN_MFG_YEAR = 1900;
public static final int MIN_SCREENSIZE = 5;

private RemoteControl mycontrol;
private int screenSize;
private String manufacturer;
private String model;
private java.util.GregorianCalendar manufactureDate;

public Television(Television aTelevision){
}

public Television(int screenSize, String manufacturer, String model, GregorianCalendar manufactureDate){
this.screenSize=screenSize;
this.manufacturer=manufacturer;
this.model=model;
this.manufactureDate= manufactureDate;
}

public void turnOn(){

}

public void turnOff(){

}

public int getScreenSize(){
return screenSize;
}

public String getManufacturer(){
return manufacturer;
}

public String getModel(){
return model;
}

public java.util.GregorianCalendar getManufactureDate(){
return manufactureDate;
}

public RemoteControl getController(){
return mycontrol;
}

public boolean equals(Object anObject){
return false; //change
}


public String toString(){
return "blabla";
}
}

Yuri said...

change
private RemoteControl; mycontrolprivate RemoteControl mycontrol = new RemoteControl();

and here is the TeleTest ot see how it works
package lab2;
import java.util.*;
public class TeleTest{
public static void main(String[]args){
Television mytv = new Television(12,"abc","abc",(GregorianCalendar)GregorianCalendar.getInstance());
System.out.println(mytv.getController());
mytv.getController().pressPowerButton();
System.out.println(mytv.getController());
}
}

Jake said...
This comment has been removed by the author.
K said...

possible RemoteControl method, thinking if we should take channel and volume in Television for static
so the RemoteControl is actually using these two field inside the Television

this is assuming channel/volume are fields in RemoteControl



package lab2;
public class RemoteControl {

//Fields
public final static int DEFAULT_CHANNEL = 0;
public final static int DEFAULT_VOLUME = 20;
public final static int MAX_CHANNEL = 200;
public final static int MAX_VOLUME = 100;
public final static int MIN_VOLUME = 0;
public final static int MIN_CHANNEL = 0;
private int channel;
private int volume;
private boolean powerState = false;



//Constructors


RemoteControl()
{
this.channel = DEFAULT_CHANNEL;
this.volume = DEFAULT_VOLUME;
}


RemoteControl(RemoteControl device)
{
RemoteControl nd = device;
this.channel = nd.channel;
this.volume = nd.volume;
this.powerState = nd.powerState;
}
//Methods


public void decreaseChannel(){
if(this.channel > MIN_CHANNEL)
this.channel --;
}



public boolean equals(Object anObject)
{
if (this.getClass()!= anObject.getClass())
return false;

RemoteControl holder = ((RemoteControl)anObject);
boolean v = this.volume == holder.volume;
boolean c = this.channel == holder.channel;
boolean p = this.powerState = holder.powerState;
boolean all = v||c||p;
if (all)
return true;
else return false;
}




public int getChannel()
{
return this.channel;
}



public int getVolume()
{
return this.volume;
}




public void increaseChannel()
{
if(this.channel < MAX_CHANNEL)
this.channel++;
}




public void increaseVolume()
{
if(this.volume < MAX_VOLUME)
this.volume ++;
}




public boolean powerIsOn()
{
return this.powerState;
}




public void pressPowerButton()
{
this.powerState = !this.powerState;
}




public void setChannel(int choice)
{
if(choice > MIN_CHANNEL||choice < MAX_CHANNEL)
this.channel = choice;
}




public String toString()
{
String s = this.getClass().getName();
s += "[powerOn=";
s += this.powerState;
s += ",volume=";
s += this.volume;
s += ",channel=";
s += this.channel;
s += "]";

return s;
}

}

K said...
This comment has been removed by the author.
K said...

found the problem X-)

forgot to turn it on !!!

so my RemoteControl should change these

public void increaseChannel()
{
if(this.channel < MAX_CHANNEL&& powerState)
this.channel++;
}


public void decreaseChannel(){
if(this.channel > MIN_CHANNEL&& powerState)
this.channel --;
}

public void increaseVolume()
{
if(this.volume < MAX_VOLUME&& powerState)
this.volume ++;
}


public void decreaseVolume()
{
if(this.volume < MAX_VOLUME&& powerState)
this.volume --;
}

K said...

okay, found out the channel and volume are actually fields in RemoteControl, I think too much.....

Neave said...

Hello everyone

Here's my code:

Tester
http://www.cse.yorku.ca/~cse63041/TVTester.java

Television
http://www.cse.yorku.ca/~cse63041/Television.java

If someone finds a mistake or doesn't understand something, reply here or send me a message (email nivm at yorku.ca or facebook message to "Niv Majar")

Anonymous said...

I like all you people. Thanks for all the help. You make these labs so much easier. Keep it up!

Anonymous said...

i decompiled the remotecontrol.class file in case anyone wanted to get a look at it and the API wasn't enough




public class RemoteControl
{

public RemoteControl()
{
tvTurnedOn = false;
channel = 1;
volume = 10;
}

public RemoteControl(RemoteControl remotecontrol)
{
tvTurnedOn = remotecontrol.tvTurnedOn;
volume = remotecontrol.volume;
channel = remotecontrol.channel;
}

public void pressPowerButton()
{
tvTurnedOn = !tvTurnedOn;
}

public boolean powerIsOn()
{
return tvTurnedOn;
}

public void increaseVolume()
{
if(tvTurnedOn && volume < 30)
volume++;
}

public void decreaseVolume()
{
if(tvTurnedOn && volume > 0)
volume--;
}

public int getVolume()
{
return volume;
}

public void increaseChannel()
{
if(tvTurnedOn)
channel = channel != 998 ? channel + 1 : 1;
}

public void decreaseChannel()
{
if(tvTurnedOn)
channel = channel != 1 ? channel - 1 : 998;
}

public void setChannel(int i)
{
if(tvTurnedOn && 1 <= i && i < 998)
channel = i;
}

public int getChannel()
{
return channel;
}

public boolean equals(Object obj)
{
if(obj == null || obj.getClass() != getClass())
{
return false;
} else
{
RemoteControl remotecontrol = (RemoteControl)obj;
return remotecontrol.tvTurnedOn == tvTurnedOn && remotecontrol.channel == channel && remotecontrol.volume == volume;
}
}

public String toString()
{
String s = getClass().getName();
s = (new StringBuilder()).append(s).append("[powerOn=").append(tvTurnedOn).append(",").toString();
s = (new StringBuilder()).append(s).append("volume=").append(volume).append(",").toString();
s = (new StringBuilder()).append(s).append("channel=").append(channel).append("]").toString();
return s;
}

public static final int MAX_VOLUME = 30;
public static final int MIN_CHANNEL = 1;
public static final int MAX_CHANNEL = 998;
public static final int DEFAULT_CHANNEL = 1;
public static final int DEFAULT_VOLUME = 10;
private boolean tvTurnedOn;
private int channel;
private int volume;
}

Anonymous said...

import java.util.*;
import java.io.*;
public class Television
{
//Constructor
public Television(int screenSize, String manufacturer, String model, GregorianCalendar manufactureDate)
{
if(screenSize >= MIN_SCREENSIZE && screenSize <= MAX_SCREENSIZE)
{
this.screenSize = screenSize;
}
else
{
this.screenSize = MIN_SCREENSIZE;
}
this.manufacturer = manufacturer;
this.model = model;

if(manufactureDate.get(Calendar.YEAR) >= MIN_MFG_YEAR)
{
int year = manufactureDate.get(Calendar.YEAR);
int month = manufactureDate.get(Calendar.MONTH);
int day = manufactureDate.get(Calendar.DAY_OF_MONTH);
this.manufactureDate = new
GregorianCalendar(year,month,day);
}

this.televisionRemoteControl = new RemoteControl();
}

public Television(Television aTelevision)
{
screenSize = aTelevision.screenSize;
manufacturer = aTelevision.manufacturer;
model = aTelevision.model;

int year = aTelevision.manufactureDate.get(Calendar.YEAR);
int month = aTelevision.manufactureDate.get(Calendar.MONTH);
int day = aTelevision.manufactureDate.get(Calendar.DAY_OF_MONTH);

manufactureDate = new GregorianCalendar(year,month,day);
televisionRemoteControl = new RemoteControl(aTelevision.getController());
}

//Methods

public RemoteControl getController()
{
return new RemoteControl(televisionRemoteControl);
}

public GregorianCalendar getManufactureDate()
{
int year = manufactureDate.get(Calendar.YEAR);
int month = manufactureDate.get(Calendar.MONTH);
int day = manufactureDate.get(Calendar.DAY_OF_MONTH);

return new GregorianCalendar(year,month,day);
}

public String getManufacturer()
{
return manufacturer;
}

public String getModel()
{
return model;
}

public int getScreenSize()
{
return screenSize;
}

public void turnOff()
{
if(televisionRemoteControl.powerIsOn())
{
televisionRemoteControl.pressPowerButton();
}
}

public void turnOn()
{
if(!(televisionRemoteControl.powerIsOn()))
{
televisionRemoteControl.pressPowerButton();
}
}

public void increaseVolume()
{
televisionRemoteControl.increaseVolume();
}

public void decreaseVolume()
{
televisionRemoteControl.decreaseVolume();
}

public void increaseChannel()
{
televisionRemoteControl.increaseChannel();
}

public void decreaseChannel()
{
televisionRemoteControl.decreaseChannel();
}

public void setChannel(int choice)
{
televisionRemoteControl.setChannel(choice);
}

public boolean equals(Object anObject)
{
if(anObject == null||anObject.getClass()!=this.getClass())
{
return false;
}
Television tel = (Television) anObject;
boolean a = (this.screenSize == tel.screenSize);
boolean b = (this.manufacturer.equals(tel.manufacturer));
boolean c = (this.model.equals(tel.model));
boolean d = (this.manufactureDate.equals(tel.manufactureDate));
return (a && b && c && d);
}

public String toString()
{
String s = getClass().getName();
s += "[ScreenSize = " + screenSize + ",";
s += "manufacturer = " + manufacturer + ",";
s += "model = " + model + ",";
s += "manufactureDate = " + manufactureDate.getTime() + "]";
return s;
}

//Instance Fields
private int screenSize = MIN_SCREENSIZE;
private String manufacturer;
private String model;
private GregorianCalendar manufactureDate;
private RemoteControl televisionRemoteControl;

public static final int MIN_SCREENSIZE = 9;
public static final int MAX_SCREENSIZE = 51;
public static final int MIN_MFG_YEAR = 1980;


}