2008年4月28日 星期一

lab class definition 2

Study Display 4.4 (2nd ed. and 3rd ed.) or Display 4.2 & Display 4.3 (1st ed.) and then
1. Comment out date.setDate(6, 17, year); by // date.setDate(6, 17, year);
2. At the next line below, add date.readInput();
3. Run the program again. Fix any problems you may encouter along the way.
4. At the last line of your program, add System.out.println(date.month);and see what happens. Why?

System.out.println()非CLASS的METHOD 故此無法純取CLASS的MEMBER



import java.util.Scanner;

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

public void setDate(int newMonth, int newDay, int newYear)
{
month = monthString(newMonth);
day = newDay;
year = newYear;
}
public String monthString(int monthNumber)
{
switch (monthNumber)
{
case 1:
return "January";
case 2:
return "Februay";
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 void writeOutput()
{
System.out.println(month + " " + day + ", "+ year);
}

}

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

public class DateThirdTryDemo

{
public static void main(String[] args)
{
DateThirdTry date = new DateThirdTry();
int year = 1882;
date.setDate(6, 17, year);

date.writeOutput();
}


}


-------------------------------------------------------------------------------------
public class DateThirdTryDemo

{
public static void main(String[] args)
{
DateThirdTry date = new DateThirdTry();

date.readInput();

System.out.println(date.month);

}

}

2008年4月14日 星期一

lab class definition

Display 4.1


Self-Test Exercise 1


public class DateFirstTry
{
public String month;
public int day;
public int year;
public void writeOutput()
{
System.out.println(month + "" + day + ", " + year);
}

public void makeItNewYears ()
{
month = "January";
day = 1;
}

}

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


public class DateFirstTryDemo
{
public static void main(String[] args)
{
DateFirstTry date1, date2;
date1 = new DateFirstTry();
date2 = new DateFirstTry();
date1.month = "December";
date1.day = 31;
date1.year = 2007;
System.out.println("date1:");
date1.writeOutput();

date2.month = "July";
date2.day = 4;
date2.year = 1776 ;
System.out.println("date2:");
date2.makeItNewYears();
date2.writeOutput();

}

Average income by gender




Write a program to calculate average income by gender based on the following data, where F stands for female and M for male.

F 62,000
M 25,000
F 38,000
F 43,000
M 65,000
M 120,000
F 80,000
M 30,100



You should be able to allow users to type in a whole line such as F 80,000 followed by next line M 30,100.

Without any change made to your program, your program should be able to process a new set of data, such as follows:

M 52,000
M 35,000
F 48,000
M 33,000
F 75,000
F 110,000
F 90,000
M 30,100

2008年4月7日 星期一

Lab 9*9


public class work
{
public static void main (String[] args)
{
int i,j;
for(i=1;i<10;i++)
{
for(j=1;j<10;j++)
{
System.out.printf("%2d*%2d=%2d", j,i,i*j);
}
System.out.println("");
}
}
}

"Lab Fibonacci numbers"


import java.util.Scanner;
public class work
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("輸入一個數字");
int a = keyboard.nextInt();
double i = 1,j = 0,k = 0,z;
for(z=0; z<=a; z++)
{
k = i+j;
if(z!= 100)

System.out.printf("%5.0e + %5.0e\t" ,j,i);

else

System.out.printf("%5.0f + %5.0f\t" ,j,i);
j = i;
i = k;

if (z!= 100)

System.out.printf("=\t%.0e\n" ,i);

else

System.out.printf("=\t%.0f\n", i);

double o=k, p=j, m=o/p;
System.out.printf("the ratio %5f\n", m);

}



}

}

Homework 3-24-2008


1. Based on your study of Display 3.8, write a code to find the max and min of a list of number.For example, given 1,3,5, and9, the max is 9 and the min is 1.Your program should be able to process a list of any length.


2. Write a program to generate the series 1, 1, 2, 3, 5, 8, 13, ...The series has a property that the third number is the sum of the first and second numbers. For example, 2=1+1, 3=1+2, and 5=2+3.

Lab Finding the max of three numbers



import java.util.Scanner;

public class work {
public static void main( String[] args )

{
Scanner keyboard = new Scanner (System.in);
System.out.println("請輸入三個數值");

double [] matrix = new double [3];
double maximum = 0;
for (int i = 0; i <>
{ matrix[i] = keyboard.nextDouble();} f
or (int i = 0; i <>
{ if (maximum < maximum =" matrix[i];">
System.out.println("最大值:" + maximum);
}
}