第三章 課後編程

歡迎大家轉載,爲保留作者成果,轉載請註明出處,http://blog.csdn.net/netluoriver,有些文件在資源中也可以下載!如果你沒有積分,可以聯繫我索要!

1、

package Threecharter;

/*
* File: InchesToCentimeters.java
* ---------------------------
* This program InchesToCentimeters on the screen.
* 
* Author luoriver
*/

import acm.program.*;
public class InchesToCentimeters extends GraphicsProgram {
	public void run(){
		println("The program is inch to centimeters");
		double feet = readDouble("Enter number of feet : " );
		double inch = readDouble("Enter number of inch : " );
		double cm = ((feet * 12) + inch ) * CENTIMETER_PER_INCH; 
		println(feet + " ft " + inch + " in = " + cm + " cm");
	}
	private static  final double CENTIMETER_PER_INCH = 2.54;
}


2、3:

package Threecharter;

/*
* File: InterRest.java
* ---------------------------
* This program calculate the rate of the deposite.
* 
* Author luoriver
*/

import acm.program.*;
public class InterRest extends GraphicsProgram {
	public void run(){
		println("Interest calculation program:");
		double balance = readDouble("Enter starting balance: " );
		double rate = readDouble("Enter annual interest rate: " );
		
		for(int i=1;i<3;i++){
		balance *= (1 + rate); 
		print("Balance after ");
		
		 	switch(i){
		 		case 1: println("one"); break;
		 		case 2: println("two") ; break;
		 	} 
		
		print("year = " + balance);
		println("");
		}
		
	}
}

4、

package Threecharter;

/*
* File: CircleArea.java
* ---------------------------
* This program calculate area of circle.
* 
* Author luoriver
*/

import acm.program.*;
import acm.graphics.*;
import acm.program.GraphicsProgram;
public class CircleArea extends GraphicsProgram {
	public void run(){
		println("Calculate the area of circle program:");
		double radius = readDouble("Enter the radius of circle: " );
		double circle_area = PI * (radius * radius);
		
		println("The Area of Circle is " + circle_area);
		
	}
// Specifies the number of PI 
private  static final double PI = 3.1415926 ;
}

5、如果是INT型會一直得0

package Threecharter;

/*
* File: CircleArea.java
* ---------------------------
* This program calculate area of circle.
* 
* Author luoriver
*/

import acm.program.*;
import acm.graphics.*;
import acm.program.GraphicsProgram;
public class FahrenheitToCelsius extends GraphicsProgram {
	public void run(){
		println("Fahrenheit convert to Celsius:");
		double fahrenheit = readDouble("Enter the fahrenheit: " );
		
		double celsius = ((double) 5/9) * (fahrenheit - 32);
				
		println("The Area of Circle is " + celsius);
		
	}
}

6、

package Threecharter;

/*
* File: Milo.java
* ---------------------------
* this is the the java sensience and art sixth
* 
* Author luoriver
*/

import acm.program.*;
import acm.graphics.*;
import acm.program.GraphicsProgram;
public class Milo extends GraphicsProgram {
	public void run(){
		double mathemagician = 4 + 9 -2 * 16 + 1/3 * 6 -67 + 8 * 2 - 3 + 26 -1/34 + 3/7 +2 -5 ;
		println("The result of Milo is " + mathemagician);
		
	}
}

The result of Milo is -50.0

7、

package Threecharter;

/*
* File: KilometerToPound.java
* ---------------------------
* This program calculate area of circle.
* 
* Author luoriver
*/

import acm.program.*;
import acm.graphics.*;
import acm.program.GraphicsProgram;
public class KilometerToPound extends GraphicsProgram {
	public void run(){
		println("此程序是將千克轉換爲磅和盎司");
		
		double kilo = readDouble("輸入重量 的單位爲千克: " );
		double pound = 2.2 * kilo ;
		double ounce = (double)kilo * pound * 16;
		
		println(kilo + "千克= " + pound + "磅 " + " = " + ounce + "盎司");
		
	}
}

8、

package Threecharter;

/*
* File: Average.java
* ---------------------------
* 這個程序計算4個數的平均值
* 
* Author luoriver
*/

import acm.program.*;
import acm.graphics.*;
import acm.program.GraphicsProgram;
public class Average extends GraphicsProgram {
	public void run(){
		println("這個程序計算4個數的平均值:");
		double total = 0;
		for(int i=1;i<5;i++){
		double value = readDouble("輸入一個值: " );
		total += value;
		
		}
		
		double average = total / 4;
		print("平均值是:" + average);
	}
}

9、無

10、這一個可能不對

package Threecharter;

/*
* File: TicToeBoard.java
* ---------------------------
* This program displays a board on the screen.
* 
* Author luoriver
*/

import java.awt.*;

import acm.graphics.*;
import acm.program.GraphicsProgram;
public class TicTacToeBoard extends GraphicsProgram {
	public void run() {
		GLine transvers1 = new GLine(getWidth()/2-BOARD_SIZE/2,getHeight()/2,getWidth()/2+BOARD_SIZE,getHeight()/2);
		transvers1.setColor(Color.RED);
		add(transvers1);
		
		GLine transvers2 = new GLine(getWidth()/2-BOARD_SIZE/2,getHeight()/2 + BOARD_SIZE/2,getWidth()/2+BOARD_SIZE,getHeight()/2+BOARD_SIZE/2 );
		transvers2.setColor(Color.RED);
		add(transvers2);
		
		GLine erect1 = new  GLine(getWidth()/2,getHeight()/2-BOARD_SIZE/2,getWidth()/2,getHeight()/2+ 2*BOARD_SIZE/2);
		erect1.setColor(Color.RED);
		add(erect1);
		
		GLine erect2 = new  GLine(getWidth()/2+ BOARD_SIZE/2,getHeight()/2-BOARD_SIZE/2,getWidth()/2+ BOARD_SIZE/2,getHeight()/2+ BOARD_SIZE);
		erect2.setColor(Color.RED);
		add(erect2);
	}
	
	private static final int BOARD_SIZE = 50 ;
}

11、無



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章