2008年6月16日 星期一

Lab Magic Parking Tower

A parking tower is out of order someday. If you park a Benz, you will end up with a Torben. Write a program to simulate this scenario. First create a class called CarParked which has a static method called outOfOrder. Name an object called yourCar, which happens to be a Benz. Your program should contain a class called CarParked and a test program called CarParkedDemo which test the method by CarParked.outOfOrder(yourCar).

Hint: You may study Display 5.14 to get some ideas.



2008年6月9日 星期一

Homework 6-02-2008

2. (30)

Write a complete Java program that uses a for loop to compute the sum of the even numbers and the sum of the odd numbers between 1 and 50.



public class Sum {
public static void main ( String [] args )
{
int sum_even = 0;
int sum_odd = 0;
int number = 1;

for (int i = 1; i <= 50; i++)
{
if(number % 2 == 0){
sum_even = sum_even + number;
}
if(number % 2 == 1){
sum_odd = sum_odd + number;
}
number ++;
}

System.out.println("Sum of even = " + sum_even);
System.out.println("Sum of odd = " + sum_odd);
}
}



3. (20) Rewrite the following for loop using a while expression.

int Sum=0;
for(i=0; i<=10; i++)
Sum=Sum+i;



public class SumUsingWhile {
public static void main ( String [] args )
{
int sum = 0;
int i = 0;
while (i <= 10)
{
sum = sum + i;
i++;
}
System.out.println("sum = " + sum);
}
}

2008年6月2日 星期一

Lab Overloading




Do Display 4.11
import java.util.Scanner;

public class DateSixthTry
{
private String month;
private int day;
private int year;

public void setDate(int monthInt, int day, int year)
{
if ( dateOK(monthInt, day , year))
{
this.month = monthString(monthInt);
this.year = year;
this.day = day;
}
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.year = year;
this.day = day;
}
else
{
System.out.println("Fatal Error");
System.exit(0);
}
}
public void setDate(int year)
{
setDate(1 ,1 ,year);
}
private boolean dateOK(int monthInt, int dayInt, int yearInt)
{
return ((monthInt >= 1) && (monthInt <= 12) && (dayInt >= 1) && (dayInt <= 31) && (yearInt >= 1000) && (yearInt <= 9999));

}
private boolean dateOK(String monthString, int dayInt , int yearInt)
{
return (monthOK(monthString) && (dayInt >= 1) && (dayInt <= 31) && (yearInt >= 1000) && (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"));

}


public void readInput()
{
boolean tryAgain = true;
Scanner keyboard = new Scanner(System.in);
while (tryAgain)
{
System.out.println("Enter month, day, year.");
System.out.println("Do not use a coma.");
String monthInput = keyboard.next();
int dayInput = keyboard.nextInt();
int yearInput = keyboard.nextInt();
if (dateOK(monthInput , dayInput , yearInput))
{
setDate(monthInput, dayInput , yearInput);
tryAgain = false;
}
else
System.out.println("Illegal date. Reenter input.");

}
}

private String monthString(int monthInt)
{
switch (monthInt)
{
case 1:
return "January";
case 2:
return "Febuary";
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";
}
}
public String toString ()
{
return month + " " + day + "," + year;
}
}


-------------------------------------------------------------------------------------

public class OverloadingDemo
{
public static void main(String[] args)
{
DateSixthTry date1 = new DateSixthTry(),date2 = new DateSixthTry(),date3 = new DateSixthTry();

date1.setDate(1, 2, 2008);
date2.setDate("February", 2, 2008);
date3.setDate(2008);

System.out.println(date1);
System.out.println(date2);
System.out.println(date3);

}



}

2008年5月26日 星期一

Lab ADT, accessor, mutator

Define a Complex class and write an object oriented program to compute (2+3i)+(4+5i) in Java.
The methods should include an access and a mutator.



2008年5月25日 星期日

lab Fraction equality test

Write a program to implement a method that can check whether 2 fractions are equal. You will implement a class called Fraction consisting of a numerator and a denominator. The equality test of 2 fractions should return a boolean value.Use the following as the tests.
1/2, 2/4
5/6, 6/7Hints:Fraction f1, f2;f1.equals(f2);

Photobucket

Photobucket

2008年5月19日 星期一

lab Fraction Addition

Write a program to implement a method that can do additions of 2 fractions. You will implement a class called Fraction consisting of a numerator and a denominator. The additions of2 fractions should be equal to a fraction.Use 1/2+1/3 as the test.Hints:Fraction f1, f2;f1.add(f2);





lab counter

Define a class called Counter whose objects count things. An object of this class records a count that is a nonnegative integer. Include methods to set the counter to 0, to increase the count by 1, and to decrease the count by 1. Include an accessor method that returns the current count value and a method that outputs the count to the screen. Write a program to test

counter.reset();
counter.inc();
counter.inc();
counter.dec();
counter.output();