CS106A 4 assugnment

 1 /*
 2  * File: FindRange.java
 3  * Name: 
 4  * Section Leader: 
 5  * --------------------
 6  * This file is the starter file for the FindRange problem.
 7  */
 8 
 9 import acm.program.*;
10 import java.io.*;
11 public class FindRange extends ConsoleProgram {
12     public void run() {
13         /* You fill this in */
14         
15         int nNumber = 0 ;
16         int nMax = 0; 
17         int nMix = 0;
18         nNumber = readInt();
19         while (true)
20         {
21             if(nNumber == 0)
22                 break ;
23             
24             if(nNumber > nMax)
25                 nMax = nNumber ;
26             if(nNumber < nMix)
27                 nMix = nNumber ;
28             nNumber = readInt();
29         }
30         print("the max number is " + nMax + "
");
31         print("the mix number is " + nMix + "
");
32         
33         
34         
35     }
36 }
 
1
/* 2 * File: FixingBrokenJava.java 3 * Name: 4 * Section Leader: 5 * 6 * This program does not work as intended. It contains both 7 * compile-time errors (errors that prevent the compiler from even 8 * running the program) and run-time errors (errors where the 9 * program does not function as intended). Your job is to fix 10 * this program so that it works correctly. Note that it is *not* 11 * sufficient to simply fix the compiler errors; you will need to 12 * update the logic as well. 13 * 14 * This program attempts to read an integer greater than one from the 15 * user, then check whether that integer is prime (whether its only 16 * divisors are 1 and itself). If so, it prints a message saying 17 * that the number is prime; otherwise it says that the number is 18 * composite. 19 */ 20 import java.io.*; 21 import acm.program.*; 22 public class FixingBrokenJava extends ConsoleProgram { 23 /* Reads a number from the user and reports whether or not it 24 * is prime. 25 */ 26 public void run() { 27 /* Get the value from the user. */ 28 int value = readInput(); 29 30 /* Check whether or not it is prime. */ 31 if (isPrime(value)) { 32 println(value + " is prime."); 33 } else { 34 println(value + " is composite."); 35 } 36 } 37 38 /** 39 * Given a positive integer, returns whether that integer is 40 * prime. 41 * 42 * @param value The value to test. 43 * @return Whether or not it is prime. 44 */ 45 private boolean isPrime(int value) { 46 /* Try all possible divisors of the number. If any of them 47 * cleanly divide the number, we return that the number is 48 * composite. 49 */ 50 for (int divisor = 0; divisor <= value; divisor++) { 51 if (value % divisor == 0) { 52 return false; 53 } 54 } 55 return true ; 56 } 57 58 /** 59 * Reads an integer greater than one from the user. 60 * 61 * @return An integer greater than one entered by the user. 62 */ 63 private int readInput() { 64 /* Get an initial value. */ 65 int value = readInt("Enter an integer greater than 1: "); 66 67 /* If the value wasn't greater than one, reprompt. */ 68 while (value < 1) { 69 println("Please enter a positive integer."); 70 value = readInt("Enter a positive integer: "); 71 } 72 73 return value; 74 } 75 }
 1 /*
 2  * File: Hailstone.java
 3  * Name: 
 4  * Section Leader: 
 5  * --------------------
 6  * This file is the starter file for the Hailstone problem.
 7  */
 8 
 9 import acm.program.*;
10 
11 public class Hailstone extends ConsoleProgram {
12     public void run() {
13         /* You fill this in */
14         print("Please Enter a Value :");
15         int nNumber = readInt();
16         while(nNumber <=1)
17         {
18             nNumber = readInt();
19         }
20         int nTemp ;
21         while(nNumber > 1)
22         {
23             nTemp = nNumber ;
24             if(nNumber % 2 == 0)
25             {
26                 nNumber  = nNumber / 2;
27                 print(nTemp + "is odd,so i take half : " + nNumber + "
");
28                 
29             }else
30             {
31                 nNumber = 3 * nNumber + 1;
32                 print(nTemp + "is even,so i make 3n + 1 : " + nNumber + "
" );
33             }
34         }
35     }
36 }
 1 /*
 2  * File: Pyramid.java
 3  * Name: 
 4  * Section Leader: 
 5  * ------------------
 6  * This file is the starter file for the Pyramid problem.
 7  * It includes definitions of the constants that match the
 8  * sample run in the assignment, but you should make sure
 9  * that changing these values causes the generated display
10  * to change accordingly.
11  */
12 
13 import acm.graphics.*;
14 import acm.program.*;
15 import java.awt.*;
16 
17 public class Pyramid extends GraphicsProgram {
18 
19     /** Width of each brick in pixels */
20     private static final int BRICK_WIDTH = 30;
21 
22     /** Height of each brick in pixels */
23     private static final int BRICK_HEIGHT = 12;
24 
25     /** Number of bricks in the base of the pyramid */
26     private static final int BRICKS_IN_BASE = 14;
27     
28     public void run() {
29         /* You fill this in. */
30         for ( int i = 0 ; i < BRICKS_IN_BASE  ; i++)
31         {
32             for ( int j = i ; j < BRICKS_IN_BASE ; j++)
33             {
34                 GRect gRect = new GRect(150 + j * BRICK_WIDTH - i*15,480-i*12,BRICK_WIDTH,BRICK_HEIGHT);
35                 add(gRect);
36             }
37         }
38     }
39 }
 1 /*
 2  * File: PythagoreanTheorem.java
 3  * Name: 
 4  * Section Leader: 
 5  * -----------------------------
 6  * This file is the starter file for the PythagoreanTheorem problem.
 7  */
 8 
 9 import acm.program.*;
10 import acm.io.*;
11 public class PythagoreanTheorem extends ConsoleProgram {
12     public void run() {
13         /* You fill this in */
14         print("Please enter A:");
15         double a = readDouble();
16         print("Please enter A:");
17         double b = readDouble();
18         double c = calculate(a,b);
19         double result = Math.sqrt(c);
20         print(result);
21     }
22     private double calculate(double a, double b) {
23         return a*a + b*b ;
24     }
25 }
 1 /*
 2  * File: Target.java
 3  * Name: 
 4  * Section Leader: 
 5  * -----------------
 6  * This file is the starter file for the Target problem.
 7  */
 8 
 9 import acm.graphics.*;
10 import acm.program.*;
11 
12 import java.io.*;
13 import java.awt.*;
14 
15 public class Target extends GraphicsProgram {    
16     public void run() {
17         /* You fill this in. */
18         
19         GOval OutOval = new GOval(320,180,SIZE,SIZE);
20         OutOval.setFillColor(Color.RED);
21         OutOval.setFilled(true);
22         add(OutOval);
23         
24         GOval sendOval = new GOval(333.5,193,SIZE*0.63,SIZE*0.63);
25         sendOval.setFillColor(Color.WHITE);
26         sendOval.setFilled(true);
27         add(sendOval);        
28         
29         GOval thirdOval = new GOval(344.5,204.5,SIZE*0.3,SIZE*0.3);
30         thirdOval.setFillColor(Color.RED);
31         thirdOval.setFilled(true);
32         add(thirdOval);        
33     }
34     private static final double SIZE = 72;
35 }
原文地址:https://www.cnblogs.com/dependence/p/4755950.html