活跃农民
- 积分
- 521
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-8-17
- 最后登录
- 1970-1-1
|
Part One: The Pythagorean Theorem
- /*
- * 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() {
- printIn ("Enter values to compute the Pythagrean Thorem.");
- double a = readDouble("a:");
- double b = readDouble("b:");
- double a2 = a*a;
- double b2 = b*b;
- double c = Math.sqrt(a2+b2);
- printIn("c="+ c);
- }
- }
复制代码 Part Two: Find Range,这个实在想不出,不知道如何能在要求用户输入后比较大小,然后循环此过程,所以参考了别人的
- /*
- * File: FindRange.java
- * Name:
- * Section Leader:
- * --------------------
- * This file is the starter file for the FindRange problem.
- */
- import acm.program.*;
- public class FindRange extends ConsoleProgram {
- private static final int SENITEL=0;
- public void run() {
- println("This program finds the largest and smallest numbers.");
- int a=readInt("?");
- if(a==0){
- println("No value.");
-
- }
- int max=a;
- int min=a;
- while(true){
- a=readInt("?");
- if(a==SENITEL)break;
-
- if(a>max){
- max=a;
- }
- if(a<min){
- min=a;
- }
-
- }
- println("smallest: "+min);
- println("largest: "+max);
- }
- }
复制代码
Part Four: Hailstone Sequence
- /*
- * 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 n= readInt("Enter a number:");
- int times = 0;
- while (n!=1){
- if (n%2==0){
- println(n+" is even so I take half: "+n/2);
- n=n/2;
- times++;}
- else {
- println(n+" is odd, so I make 3n+1: "+(n*3+1));
- n=n*3+1;
- times++;
- }
- }
- println("The process took "+ times+"to reach 1");
- }
- }
复制代码 Part Five: Artistry!
- /*
- * File: Artistry.java
- * Name:
- * Section Leader:
- * ==========================================================
- * Replace these comments with a description of your program.
- * Since this program is more freeform than the rest, tell us
- * a bit about it in these comments!
- */
- import java.awt.Color;
- import acm.program.*;
- import acm.graphics.*;
- public class Artistry extends GraphicsProgram {
- public void run() {
- /* You fill this in. Remember that you must have
- *
- * 1. At most twenty GObjects,
- * 2. At least one filled object,
- * 3. At least two different colors of objects, and
- * 4. At least three different types of objects (not
- * counting the GLabel you use to sign your name).
- *
- * Also, be sure to sign your name in the bottom-right
- * corner of the canvas.
- */
- GRect rect1 = new GRect(50,50,150,30);
- rect1.setFillColor(Color.black);
- rect1.setFilled(true);
- add(rect1);
-
- GRect rect2 = new GRect(250,50,150,30);
- rect2.setFillColor(Color.black);
- rect2.setFilled(true);
- add(rect2);
-
- GRect rect3 = new GRect(200,200,50,30);
- rect3.setFillColor(Color.black);
- rect3.setFilled(true);
-
-
- GOval oval1 = new GOval(200,220,40,30);
- oval1.setFillColor(Color.cyan);
- oval1.setFilled(true);
- add(oval1);
-
- GRect rect4 = new GRect(450,50,20,250);
- rect4.setFillColor(Color.black);
- rect4.setFilled(true);
- add(rect4);
-
- GRect rect5 = new GRect(500,50,20,250);
- rect5.setFillColor(Color.black);
- rect5.setFilled(true);
- add(rect5);
-
- GLabel label1 = new GLabel("AWKWARD", 550,300);
- label1.setFont("SansSerif-36");
- label1.setColor(Color.blue);
- add(label1);
-
- GLabel label2 = new GLabel("Artistry by Julia", 630,450);
- add(label2);
-
-
-
- }
- }
复制代码
Part Six: Target
- /*
- * 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() {
- GOval ovalOut = new GOval((getWidth()-72)/2,(getHeight()-72)/2,72,72);
- ovalOut.setFillColor(Color.red);
- ovalOut.setFilled(true);
- add(ovalOut);
-
- GOval ovalMid = new GOval((getWidth()-46.8)/2,(getHeight()-46.8)/2,46.8,46.8);
- ovalMid.setFillColor(Color.white);
- ovalMid.setFilled(true);
- add(ovalMid);
-
- GOval ovalInter = new GOval((getWidth()-21.6)/2,(getHeight()-21.6)/2,21.6,21.6);
- ovalInter.setFillColor(Color.red);
- ovalInter.setFilled(true);
- add(ovalInter);
- }
- }
复制代码
Part Seven: Pyramid双重循环这个想了好久,最终还是有借鉴他人的思路
- /*
- * 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;
- /** Height 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() {
- for (int i=1; i<=BRICKS_IN_BASE; i++){
- for (int n=1; n<=i; n++){
- GRect rect = new GRect((getWidth()-BRICK_WIDTH*i)/2+BRICK_WIDTH*(n-1),getHeight()-BRICK_HEIGHT*(BRICKS_IN_BASE-i+1),BRICK_WIDTH,BRICK_HEIGHT);
- add(rect);
- }
- }
- }
-
-
- }
复制代码
|
|