顯示具有 作業 標籤的文章。 顯示所有文章
顯示具有 作業 標籤的文章。 顯示所有文章

2009年6月8日 星期一

Homework 6-1-2009 Modular Sorting

Write a sort method which takes a double array as parameter
and returns the sorted array. Call this method in a main program.

Hint: The lab is a rewriting of Lab Sorting
to make the sorting procedure a reusable method.



-----.





----
package javaapplication8;

/**
*
* @author KFN
*/
public class Sort
{

/** Creates a new instance of Sort */
public Sort()
{
}

public static void sort( double[] score ) {
int index = 0;
double temp = 0.0;
for( index = score.length-1; index >-1 ; --index)
{
for (int j = 0; j < index; j++)
{
if(score[index] > score[j])
{
temp = score[j];
score[j] = score[index];
score[index] = temp;
}
}
}


}

}

---
package javaapplication8;

/**
*
* @author KFN
*
*/
import java.util.Scanner;
public class ArrayOfScores
{

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double[] score = new double[5];
int index;
double max, temp;

System.out.println("Enter 5 Scores");
score[0] = keyboard.nextDouble();
max = score[0];
for(index = 1; index < score.length; ++index)
score[index] = keyboard.nextDouble();

Sort.sort( score );
/*
Sort a = new Sort();
a.sort( score );
*/
System.out.println("The scores are" );
for(index = 0; index< score.length; index++)
System.out.println(score[index]);

2009年5月11日 星期一

Homework 5-4-2009

Do Project 7 of Chapter 4.

1.
2.
3.4.




---



2009年5月4日 星期一

Homework 4-27-2009

Do project 2 of Chapter 4.

強烈要求同學一定要親自動手做,鼓勵同學將理論與實作密切配合。
相信只要同學真正的努力用功,縱使資質稍差,應該都可以學到Java程式設計之知識



homework

/*
* Fraction.java
*
* Created on 2009年5月4日, 下午 5:18
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package fraction;

/**
*
* @author KFN
*/
public class Fraction
{
private int numerator;
private int denominator;


public void number( int numerator, int denominator)
{
this.numerator = numerator;
this.denominator = denominator;
}
/**
* Creates a new instance of Fraction
*/
public String outputstring()
{

return (numerator + "/" + denominator); //分數的表示形式
}
public Fraction multiply(Fraction Fraction2)
{
Fraction countOfTemp = new Fraction();
countOfTemp.numerator = numerator*Fraction2.numerator;
countOfTemp.denominator = denominator*Fraction2.denominator;

int d = 0;
int a = countOfTemp.numerator;
int b = countOfTemp.denominator;
int c = 0;
do {

c = a % b;
int temp = a%b;
a = b;
b = temp;
d = a;

} while ( c != 0 );// end while
countOfTemp.numerator = countOfTemp.numerator / d;
countOfTemp.denominator = countOfTemp.denominator / d;
return countOfTemp;
}
public Fraction()
{
}

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Fraction f1 = new Fraction();
Fraction f2 = new Fraction();
f1.number(1, 2);
f2.number(1, 3);
System.out.println("f1 = " + f1.outputstring() + "\nf2 = " + f2.outputstring());
System.out.println("f1 * f2 = "+ f1.multiply(f2).outputstring());
// TODO code application logic here
}

}

Homework 4-13-2009 Fraction Multiplication

Write a program to implement a method that can multiply 2 fractions. You will implement a class called Fraction consisting of a numerator and a denominator. The multiplication of
2 fractions should be equal to a fraction.
Use 1/2 * 1/3 as the test.

Hints:
Fraction f1, f2;
f1.multiply(f2);





改成1/2 * 2/3


due date: 4/27/2009

沒有在期限內做出來
卡在通分卡太久
又死撐著不去看別人怎麼寫
不算藉口的藉口...

2009年4月12日 星期日

Homework 3-30-2009: 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();


2009年3月23日 星期一

Homework 3-16-2009

1. Project 7 of Chap. 3

2. Write a program to generate the following table of arithmetic expressions

1*1=1 1*2=2 1*3=3 ... 1*9=9
2*1=2 2*2=4 2*3=6 ... 2*9=19
...
9*1=9 9*2=18 9*3=27 ... 9*9=81

2009年3月15日 星期日

Homework 3-9-2009

1. Do Project 4 in Chapter 2

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//new class為配置一個記憶體空間
System.out.println("Enter the distance of the commute in miles.");
double a = keyboard.nextDouble();
System.out.println("Enter the feul consumption rate in miles per gallon.");
double b = keyboard.nextDouble();
System.out.println("Enter the price of a gallon.");
double c = keyboard.nextDouble();
System.out.println("The cost of the commute is \n" + ( a / b ) * c );
// TODO code application logic here
}
2. Do Project 5 in Chapter 2

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter the purchase price of an item.");
double P = keyboard.nextDouble();
System.out.println("Enter the exceptd number of years of service.");
double Y = keyboard.nextDouble();
System.out.println("Enter the expected salvage value.");
double S = keyboard.nextDouble();
System.out.println("The yearly depreciation for the item is \n" + (P - S ) / Y );
// TODO code application logic here
}
3. Do Project 6, in Chapter 2

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter the amount of artificial sweetener needed to kill a mouse.(g)");
double S = keyboard.nextDouble();
System.out.println("Enter the weight of the mouse.(g)");
double m = keyboard.nextDouble();
System.out.println("Enter the desired weight of the dieter.(g)");
double M = keyboard.nextDouble();
System.out.println("The diet soda pop it is possible to dink is\n" + (M / m ) * S *1000 + "ml or" + (M / m ) * S + "L");
// TODO code application logic here
}
4. Do Project 7, in Chapter 2

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter the price of item \n (from 25 cents to a dollar, in 5-cent increments)");
int item = keyboard.nextInt();
System.out.println("You buoght an item for"+ item +"cents and gave me a dollar");

int q = (100 - item)/25;
int n = ((100 - item)%25)/5;

System.out.println("So your change is \n"+ q +" quarters and "+ n +" nickels.");

// TODO code application logic here
}

2009年3月9日 星期一

Homework 3-2-2009

1. Write a program that can reverse the order of an input string. For example, if you input "ab", it will output "ba". If you input "abcdefg", it should return "gfedcba".

概念:
比如我有6個字,所以.length()=6
但是我要從編號5開始倒著排回去(5 4 3 2 1 0)
所以.length()-1並取for迴圈美執行ㄧ次減一個編號這樣(--)

public static void main(String[] args) {
String word = "ab";
System.out.println(word);
for(int i=word.length()-1;i>=0;i--)
{
System.out.print(word.charAt(i));
}

String word2 = "abcdefg";
System.out.println("\n" + word2);
for(int i=word2.length()-1;i>=0;i--)
{
System.out.print(word2.charAt(i));
}

// TODO code application logic here
}


2. Write a program that can print names alternatively. The names are in the format of "First Name + Last Name" or "Last Name, First Name". For example, if you input "Walt Savitch", your program will output "Savitch, Walt". If you input "Savitch, Walt", your program will output "Walt Savitch".


用,來取位置來交換前半段和後半段

public static void main(String[] args) {
String name = "Dispy,Jhung";
System.out.println(name);
int i = name.indexOf(",");
String start = name.substring(i+1);
String end = name.substring(0,i);
name = start+" "+end;
System.out.println(name);

2009年3月2日 星期一

Homework 2-23-2009

1. Suppose you are a landscape architect who charges $5,000 per mile to landscape a highway, and suppose you know the length in feet of the high way you are working on. Write a Java program to calculate the price you charge when the length is 6000 and 4000, respectively.

Hint: There are 5280 feet in a mile.

2. Write a Java program that displays the results of the expressions 15/4, 4/15, 15%4, 4%15. Calculate the values of these expressions manually to verify that the displayed values are correct.