第三章 课后编程

欢迎大家转载,为保留作者成果,转载请注明出处,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、无



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值