2005/12/19

Homework 12-12-2005 Counter II

package s9226262;

import javax.swing.JOptionPane;
public class Counter
{
int count;
public void setToZero()
{ count=0; }

public void increase()
{ count+=1; }

public void decrease()
{
count-=1;
if ( count<0>
{ System.out.println("Error"); }
}

public int accessor()
{ return count; }

public void screen()
{ System.out.println("The Count2 = "+count); }

public boolean equals(Counter Count)
{ return this.count == Count.count; }

public String toString()
{ return Integer.toString(count); }
}




package
s9226262;

public class CounterDemo
{
public static void main(String[] args)
{
Counter count1 = new Counter();
Counter count2 = new Counter();
count1.setToZero();
count2.setToZero();
count1.increase();
count2.increase();
int Count1 = count1.accessor();
System.out.println("Return Count1 =" + Count1);
count2.decrease();
count2.screen();
System.out.println("Is Count1 = Count2 ?? is "+ count1.equals(count2));
System.out.println("");
System.out.println("ToString Coun1 & Count2:");
System.out.println("Count1: "+ count1.toString());
System.out.println("Count2: "+ count2.toString());
}
}

Lab 12-12-2005 (2) Static Class

package welcome;

import javax.swing.JOptionPane;
public class Lab_Fibonacci
{
public static double f1=1,f2=1,f3=0;
public static void main(String[] args)
{
String num_str = JOptionPane.showInputDialog("Enter the items number n :");
System.out.println( Lab_Fibonacci.next(num_str) );
System.exit(0);
}

public static double next(String num_str)
{
int n = Integer.parseInt(num_str);
for ( double i=1;i<=n;i++ )
{
f3 = f1 + f2;
f1 = f2;
f2 = f3;
}
return f3;
}

}

Lab 12-12-2005 (1) Constructor

package welcome;

import java.io.*;
public class Date
{
private String month;
private int day;
private int year;

public Date( )
{
month = "January";
day = 1;
year = 1000;
}

public Date(int monthInt, int day, int year)
{ setDate(monthInt, day, year); }

public Date(String monthString, int day, int year)
{ setDate(monthString, day, year); }

public Date(int year)
{ setDate(1, 1, year); }

public Date(Date aDate)
{
if (aDate == null)
{
System.out.println("Fatal Error.");
System.exit(0);
}
month = aDate.month;
day = aDate.day;
year = aDate.year;
}

public void setDate(int monthInt, int day, int year)
{
if (dateOK(monthInt, day, year))
{
this.month = monthString(monthInt);
this.day = day;
this.year = year;
}
else
{
System.out.println("Fatal Error");
System.exit(0);
}
}

public void setDate(String monthString, int day, int year)
{
if (dateOK(monthString, day, year))
{
this.month = monthString;
this.day = day;
this.year = year;
}
else
{
System.out.println("Fatal Error");
System.exit(0);
}
}

public void setDate(int year)
{ setDate(1, 1, year); }

public void setYear(int year)
{
if ( (year <> 9999) )
{
System.out.println("Fatal Error");
System.exit(0);
}
else
this.year = year;
}

public void setMonth(int monthNumber)
{
if ((monthNumber <= 0) || (monthNumber > 12))
{
System.out.println("Fatal Error");
System.exit(0);
}
else
month = monthString(monthNumber);
}

public void setDay(int day)
{
if ((day <= 0) || (day > 31))
{
System.out.println("Fatal Error");
System.exit(0);
}
else
this.day = day;
}

public int getMonth( )
{
if (month.equals("January"))
return 1;
else if (month.equals("February"))
return 2;
else if (month.equalsIgnoreCase("March"))
return 3;
else if (month.equalsIgnoreCase("April"))
return 4;
else if (month.equalsIgnoreCase("May"))
return 5;
else if (month.equals("June"))
return 6;
else if (month.equalsIgnoreCase("July"))
return 7;
else if (month.equalsIgnoreCase("August"))
return 8;
else if (month.equalsIgnoreCase("September"))
return 9;
else if (month.equalsIgnoreCase("October"))
return 10;
else if (month.equals("November"))
return 11;
else if (month.equals("December"))
return 12;
else
{
System.out.println("Fatal Error");
System.exit(0);
return 0;
}
}

public int getDay( )
{ return day; }

public int getYear( )
{ return year; }

public String toString( )
{ return (month + " " + day + ", " + year); }

public boolean equals(Date otherDate)
{
return ( (month.equals(otherDate.month)) &&amp;amp; (day == otherDate.day) && (year == otherDate.year) );
}

public boolean precedes(Date otherDate)
{
return ( (year < year ="=">
|| (year == otherDate.year && month.equals(otherDate.month)&& day <>
}

public void readInput( ) throws IOException
{
boolean tryAgain = true;
BufferedReader keyboard= new BufferedReader(new
InputStreamReader(System.in));

while (tryAgain)
{
System.out.println("Enter month, day, and year.");
System.out.println("Do not use a comma.");
String monthInput = keyboard.readLine( );
int dayInput = keyboard.read( );
int yearInput = keyboard.read( );
if (dateOK(monthInput, dayInput, yearInput) )
{
setDate(monthInput, dayInput, yearInput);
tryAgain = false;
}
else
System.out.println("Illegal date. Reenter input.");
}
}

private boolean dateOK(int monthInt, int dayInt, int yearInt)
{
return ( (monthInt >= 1) &&amp;amp; (monthInt <= 12) && (dayInt >= 1) && (dayInt <= 31) &&
(yearInt >= 1000) &&amp;amp; (yearInt <= 9999) );
}

private boolean dateOK(String monthString, int dayInt, int yearInt)
{
return ( monthOK(monthString) &&amp;amp; (dayInt >= 1) && (dayInt <= 31) && (yearInt >= 1000) &&amp; (yearInt <= 9999) );
}

private boolean monthOK(String month)
{
return (month.equals("January") || month.equals("February") ||
month.equals("March") || month.equals("April") ||
month.equals("May") || month.equals("June") ||
month.equals("July") || month.equals("August") ||
month.equals("September") || month.equals("October") ||
month.equals("November") || month.equals("December") );
}

private String monthString(int monthNumber)
{
switch (monthNumber)
{
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default:
System.out.println("Fatal Error");
System.exit(0);
return "Error";
}
}


}

package welcome;

public class ConstructorsDemo
{
public static void main(String[] args)
{
Date date1 = new Date("December", 16, 1770),
date2 = new Date(12, 27, 1756),
date3 = new Date(1882),
date4 = new Date();

System.out.println("Whose birthday is " + date1 + "?");
System.out.println("Whose birthday is " + date2 + "?");
System.out.println("Whose birthday is " + date3 + "?");
System.out.println("The default date is " + date4 + ".");
}
}

Homework 12-5-2005

package welcome;

public class Temp
{


private double value;
private char scale;

public void writeout()
{
System.out.println(value + " " + scale);
}


public Temp()
{
this.value = 0;
this.scale = 'C';
}

public Temp(double value)
{
this.value = value;
this.scale = 'C';
}

public Temp(char scale)
{
this.value = 0;
if (scale == 'f' || scale == 'F')
{ this.scale = 'F'; }
else if (scale == 'c' || scale == 'C')
{ this.scale = 'C'; }
else
{
System.out.println("Error ");
this.scale = 'C';
}
}

public Temp(double value, char scale)
{
this.value = value;
if (scale == 'f' || scale == 'F')
{ this.scale = 'F'; }
else if (scale == 'c' || scale == 'C')
{ this.scale = 'C'; }
else
{
System.out.println("Error ");
this.scale = 'C';
}
}



public double getC()
{
double Tc = 0;
if (scale == 'C')
{ Tc = value; }
else if (scale == 'F')
{ Tc = (value - 32) * 5 / 9; }
return Tc;
}

public double getF() {
double Tf = 0;
if (scale == 'F')
{ Tf = value; }
else if (scale == 'C')
{ Tf = (value * 9 / 5) + 32; }
return Tf;
}

public void setValue(double value)
{ this.value = value; }

public void setScale(char scale)
{

if (scale == 'f' || scale == 'F')
{ this.scale = 'F'; }
else if (scale == 'c' || scale == 'C')
{ this.scale = 'C'; }
else
{ System.out.println("Error "); }

}

public void setTemp(double value, char scale)
{
if (scale == 'f' || scale == 'F')
{ this.scale = 'F'; }
else if (scale == 'c' || scale == 'C')
{ this.scale = 'C'; }
else
{ System.out.println("Error "); }
this.value = value;
}

public boolean equals(Temp otherTemp)
{
if ( this.scale == 'C' && this.value == otherTemp.getC() )
{ return true; }
else if ( this.scale == 'F' && this.value == otherTemp.getF() )
{ return true; }
else
{ return false; }
}

public boolean greater(Temp otherTemp)
{
if ( this.scale == 'C' && this.value >= otherTemp.getC() )
{ return true; }
else if ( this.scale == 'F' && this.value >= otherTemp.getF() )
{ return true; }
else
{ return false; }
}

public boolean smaller(Temp otherTemp)
{
if ( this.scale == 'C' && this.value <= otherTemp.getC() )
{ return true; }
else if ( this.scale == 'F' && this.value <= otherTemp.getF() )
{ return true; }
else
{ return false; }
}


}







package welcome;


public class TempDisplay
{

public static void main(String[] args)
{

Temp Tc_mp = new Temp(0.0, 'C');
Temp Tf_mp = new Temp(32.0, 'F');

Temp Tc_bp = new Temp(100.0);
Tc_bp.setScale('C');

Temp Tf_bp = new Temp(212.0);
Tf_bp.setScale('F');

Temp Tc_cold = new Temp();
Tc_cold.setTemp( -40.0, 'C');

Temp Tf_cold = new Temp();
Tf_cold.setScale('F');
Tf_cold.setValue( -40.0);

System.out.print("The Tc of melt point = ");
Tc_mp.writeout();
System.out.print("The Tf of melt point = ");
Tf_mp.writeout();
System.out.print("The Tc of boil point = ");
Tc_bp.writeout();
System.out.print("The Tf of boil point = ");
Tf_bp.writeout();
System.out.print("The Tc of cold = ");
Tc_cold.writeout();
System.out.print("The Tf of cold = ");
Tc_cold.writeout();

System.out.print("\nTest equals.\n");
System.out.println("Tf_bp" + " = " + "Tc_bp" + " ? " + Tf_bp.equals(Tc_bp));
System.out.println("Tc_mp" + " = " + "Tf_mp" + " ? " + Tc_mp.equals(Tf_mp));
System.out.println("Tc_cold" + " = " + "Tc_cold" + " ? " + Tf_cold.equals(Tc_cold));
System.out.println("Tf_mp" + " = " + "Tc_mp" + " ? " + Tf_mp.equals(Tc_mp));

System.out.print("\nTest smaller.\n");
System.out.println("Tc_mp" + " <= " + "Tc_cold" + " ? " + Tc_mp.smaller(Tc_cold));
System.out.println("Tc_mp" + " <= " + "Tf_bp" + " ? " + Tc_mp.smaller(Tf_bp));
System.out.println("Tf_mp" + " <= " + "Tc_cold" + " ? " + Tf_mp.smaller(Tc_cold));
System.out.println("Tf_mp" + " <= " + "Tc_bp" + " ? " + Tf_mp.smaller(Tc_bp));

System.out.print("\nTest geater\n");
System.out.println("Tc_mp" + " >= " + "Tf_bp" + " ? " + Tc_mp.greater(Tc_bp));
System.out.println("Tc_mp" + " >= " + "Tf_cold" + " ? " + Tc_mp.greater(Tf_cold));
System.out.println("Tf_mp" + " >= " + "Tf_bp" + " ? " + Tf_mp.greater(Tf_bp));
System.out.println("Tf_mp" + " >= " + "Tf_cold" + " ? " + Tf_mp.greater(Tf_cold));
}

}