中级农民
- 积分
- 255
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2013-1-7
- 最后登录
- 1970-1-1
|
/*
* File: PythagoreanTheorem.java
* Name:
* Section Leader:
* -----------------------------
* This file is the starter file for the PythagoreanTheorem problem.
*/
import acm.program.*;
public class PythagoreanTheorem extends ConsoleProgram {
public void run() {
println("Welcome to compute Pythagorean Theorem");
int x = readInt ("a:");
int y = readInt ("b:");
double z = Math.sqrt(x*x+y*y);
println ("c = "+z);
}
}
/*
* File: Hailstone.java
* Name:
* Section Leader:
* --------------------
* This file is the starter file for the Hailstone problem.
*/
import acm.program.*;
public class Hailstone extends ConsoleProgram {
public void run() {
int x = readInt ("number:");
int y=0;
while (true){
if (x==1){
break;
}
if (x%2==0){
y=x;
x=x/2;
println (y+" is oven, so I take half: "+x);
}else{
y=x;
x=x*3+1;
println (y+" is odd, so make 3n+1: "+x);
}
}
}
}
/*
* File: FindRange.java
* Name:
* Section Leader:
* --------------------
* This file is the starter file for the FindRange problem.
*/
import acm.program.*;
public class FindRange extends ConsoleProgram {
public void run() {
int x =readInt("?");
if (x == 0) {
println ("The number you entered is 0");
}
int l=x;
int s=x;
while (x !=y){
x =readInt("?");
if (x==y) {
break;
} else if (x>l) {
l=x;
} else if (x<s){
s=x;
} else {
}
}
if (l !=s){
println ("The largest number is: "+l+".");
println ("The smallest number is: "+s+".");
}else if (l==s){
println (s+" is both the largest and the smallest number.");
}
}
private static int y=0;
}
/*
* File: Target.java
* Name:
* Section Leader:
* -----------------
* This file is the starter file for the Target problem.
*/
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class Target extends GraphicsProgram {
public void run() {
double y= getHeight();
double x= getWidth();
GOval a = new GOval((x-144)/2,(y-144)/2,144,144);
a.setColor(Color.red);
a.setFilled(true);
a.setFillColor (Color.red);
add(a);
GOval b = new GOval((x-100)/2,(y-100)/2,100,100);
b.setColor(Color.white);
b.setFilled(true);
b.setFillColor (Color.white);
add(b);
GOval c = new GOval((x-44)/2,(y-44)/2,44,44);
c.setColor(Color.red);
c.setFilled(true);
c.setFillColor (Color.red);
add(c);
}
}
/*
* File: ProgramHierarchy.java
* Name:
* Section Leader:
* ---------------------------
* This file is the starter file for the ProgramHierarchy problem.
*/
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class ProgramHierarchy extends GraphicsProgram {
private static final int height=50;
private static final int length=100;
public void run() {
double x=getWidth();
double y=getHeight();
GRect a = new GRect ((x-length)/2,(y-3*height)/2,length,height);
add(a);
GRect b = new GRect ((x-length)/2,(y-3*height)/2+length,length,height);
add(b);
GRect c = new GRect ((x-length)/2-3*height,(y-3*height)/2+length,length,height);
add(c);
GRect d = new GRect ((x-length)/2+3*height,(y-3*height)/2+length,length,height);
add(d);
GLine ab = new GLine(x/2,(y-3*height)/2+height,x/2,y/2+height/2);
add(ab);
GLine ac = new GLine(x/2,(y-3*height)/2+height,x/2-length-height,y/2+height/2);
add(ac);
GLine ad = new GLine(x/2,(y-3*height)/2+height,x/2+length+height,y/2+height/2);
add(ad);
GLabel aa = new GLabel("Program");
GLabel bb = new GLabel("GraphicsProgram");
GLabel cc = new GLabel("ConsoleProgram");
GLabel dd = new GLabel("DialogProgram");
add(aa,(x-aa.getWidth())/2,y/2-50+aa.getHeight()/2);
add(bb,(x-bb.getWidth())/2-3*bb.getWidth()/2,y/2-50+bb.getHeight()/2+2*height);
add(cc,(x-cc.getWidth())/2,y/2-50+cc.getHeight()/2+2*height);
add(dd,(x-dd.getWidth())/2+3*bb.getWidth()/2,y/2-50+dd.getHeight()/2+2*height);
}
}
/*
* File: Pyramid.java
* Name:
* Section Leader:
* ------------------
* This file is the starter file for the Pyramid problem.
* It includes definitions of the constants that match the
* sample run in the assignment, but you should make sure
* that changing these values causes the generated display
* to change accordingly.
*/
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class Pyramid extends GraphicsProgram {
/** Width of each brick in pixels */
private static final int BRICK_WIDTH = 30;
/** Width of each brick in pixels */
private static final int BRICK_HEIGHT = 12;
/** Number of bricks in the base of the pyramid */
private static final int BRICKS_IN_BASE = 14;
public void run() {
int x = getWidth();
int y = getHeight();
for (int i=14;i>0;i--){
for (int j=0;j<i;j++){
GRect a = new GRect (x/2-7*30+15*(14-i)+j*30,y-12*(14-i),30,12);
add(a);
}
}
}
}
|
|