摘要:1 /* 2 * File: PigLatin.java 3 * -------------- 4 * This file takes a line of text and converts each word into Pig Latin. 5 * The rules for forming Pig Latin words are as follows: 6 * - If the word begins with a vowel, add "way" to the end of the word. 7 * - If the word begins with a conso
阅读全文
摘要:import acm.util.*;import acm.program.*;//creates random wordpublic class CreateRandomWord extends ConsoleProgram { public void run(){ for(int i = 0; i < 5; i++) { println(randomWord()); } } private String randomWord(){ String result = ""; for(int i...
阅读全文
摘要:1 import acm.graphics.*; 2 import acm.program.*; 3 import acm.gui.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 //单位转换程序; 8 public class LengthConverter extends Program { 9 //初始化界面; 10 public void init() { 11 setLayout(new TableLayout(3, 2)); 12 lengthC...
阅读全文
摘要:1 import acm.graphics.*; 2 import acm.program.*; 3 4 public class imageConvert extends GraphicsProgram { 5 public void run(){ 6 GImage image = new GImage("Penguins.jpg"); 7 image = applyAveragingFilter(image); 8 add(image); 9 }10 /*11 * Creates a new image ...
阅读全文
摘要:import java.lang.Runtime;import acm.program.*;public class GCTest extends ConsoleProgram { public void run() { Runtime myRuntime = Runtime.getRuntime(); long oldMemory = myRuntime.freeMemory(); for(int i = 0; i < 10000; i++){ new Rational(1); } myRunt...
阅读全文
摘要:1 /** 2 * The Rational class is used to represent rational numbers, which 3 * are defined to be the quotient of two integer. 4 * @author Administrator 5 * 6 */ 7 public class Rational { 8 /** Creates a new Rational from the integer argument. */ 9 public Rational(){ 10 ...
阅读全文
摘要:import acm.program.*;//输入16进制输出10进制public class HexToDecimalConverter extends ConsoleProgram { public void run(){ println("This program converts hexadecimal to decimal."); println("Enter 0 to stop."); while(true){ String line = readLine("Enter a hexdecimal number: ...
阅读全文
摘要:创建一个简单的绘图程序,可以绘制矩形,椭圆和直线。1.先生成程序界面:创一个添加显示图片按钮的方法:1 private JButton addButton(String str){2 ImageIcon icon = new ImageIcon(str);//按钮图标3 JButton button = new JButton(icon);4 button.setContentAreaFilled(false);//按钮透明;5 return button;6 }初始化界面: 1 public void init(){ 2...
阅读全文
摘要:1 /* 2 * 程序可以用鼠标拖拽话出一个矩形; 3 */ 4 import acm.graphics.*; 5 import acm.program.*; 6 import java.awt.event.*; 7 8 public class DrawRectangle extends GraphicsProgram{ 9 //鼠标侦听10 public void run(){11 addMouseListeners();12 }13 //鼠标按下事件14 public void mousePressed(MouseEvent...
阅读全文
摘要:1 /* 2 * 程序可以用鼠标拖拽话出一个矩形; 3 */ 4 import acm.graphics.*; 5 import acm.program.*; 6 import java.awt.event.*; 7 8 public class DrawRectangle extends GraphicsProgram{ 9 //鼠标侦听10 public void run(){11 addMouseListeners();12 }13 //鼠标按下事件14 public void mousePressed(MouseEvent...
阅读全文
摘要:1 /* 2 * 程序运行时在鼠标的左边生成一个显示鼠标坐标的GLabel,并跟着鼠标 3 * 移动。 4 */ 5 import acm.graphics.*; 6 import acm.program.*; 7 import java.awt.event.*; 8 9 public class MouseTracker extends GraphicsProgram{10 public void run(){11 addMouseListeners();12 label = new GLabel("");13 }14 pub...
阅读全文
摘要:1 import acm.program.*; 2 import java.awt.event.*; 3 import java.awt.*; 4 import acm.util.*; 5 import javax.swing.*; 6 import acm.gui.*; 7 /* 程序实现一个简单的四则运算计算器;*/ 8 public class Calculator extends Program { 9 /* 初始的用户界面 */ 10 public void init(){ 11 setLayout(new TableLayout(...
阅读全文
摘要:1 import acm.graphics.*; 2 import acm.program.*; 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 /** 7 * This program creates a five-pointed star every time the 8 * user clicks the mouse on the canvas. 9 * @author Administrator10 *11 */12 public class DrawStarMap ext...
阅读全文
摘要:初始需要定义的一些常量,和使用的库。 1 import acm.graphics.*; 2 import acm.program.*; 3 import acm.util.*; 4 5 import java.applet.*; 6 import java.awt.*; 7 import java.awt.event.*; 8 9 public class Breakout extends GraphicsProgram {10 11 /** Width and height of application window in pixels */12 public static fi...
阅读全文
摘要:1 /* 2 * File: RandomCircles.java 3 * ------------------------ 4 * 程序绘制10个大小、颜色、位置随机的圆。 5 */ 6 import acm.graphics.*; 7 import acm.program.*; 8 import acm.util.*; 9 10 public class RandomCircles extends GraphicsProgram {11 private static final int NUMBER = 10;12 private static final dou...
阅读全文
摘要:1 /* 2 *File: MathQuiz.java 3 */ 4 import acm.util.*; 5 import acm.program.*; 6 7 public class MathQuiz extends ConsoleProgram{ 8 public void run(){ 9 println("Welcome to MathQuiz ");10 for (int i = 0; i < TIMES; i++ ) {11 doOneQuiz();12 }13 14 }15 p...
阅读全文
摘要:1 /* 2 * File:GambleMachine.java 3 * ------------------------ 4 * 模拟“赌博机” 5 */ 6 import acm.util.*; 7 import acm.program.*; 8 9 public class GambleMachine extends ConsoleProgram { 10 public void run(){ 11 String ask = "Would you like to play? "; 12 if(readLine("Wou...
阅读全文
摘要: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 println("This program sh...
阅读全文
摘要:画一个不停反弹的小球。 1 /* 2 * File:BouncingBall.java 3 * ------------------- 4 * This program draw a bouncing ball. 5 */ 6 7 import acm.program.*; 8 import acm.graphics.*; 9 import java.awt.*;10 11 public class BouncingBall extends GraphicsProgram {12 13 public void run(){14 int x = (get...
阅读全文
摘要:1 /* 2 * File:CheckBoard.java 3 * -------------------- 4 * 画出国际象棋的棋盘,使用嵌套的for循环。 5 */ 6 7 import acm.graphics.*; 8 import acm.program.*; 9 import java.awt.*;10 11 public class CheckBoard extends GraphicsProgram {12 13 public void run(){14 double sqSize = (double)(getHeight() / ...
阅读全文
摘要:1 /* 2 * 画出一个用矩形砖块的金字塔,每行递减1 3 */ 4 5 import acm.program.*; 6 import acm.graphics.*; 7 8 public class drawPyramid extends GraphicsProgram { 9 10 public void run(){11 double x = (getWidth() - BRICK_IN_BASE * BRICK_WIDTH) / 2;12 double y = (getHeight() + BRICK_IN_BASE * BR...
阅读全文
摘要:1 /* 2 * File:GLineExample.java 3 * ---------------------- 4 * This program draws a Tic-Tac-Toe board as an illustration 5 * of the GLine class. The version uses explict coordinate 6 * values which makes the program difficult to extand or 7 * maintain. In Chapter 3, you will learn how to cons...
阅读全文
摘要:好的编程风格是程序设计很重要的一部分,好的编程风格可以简化未来维护工作的复杂,减少软件维护、改进的成本。 1.可读性 程序应该要以方便人们阅读的方法来编写,方便将来其他人能够维护你所编写的代码,所以必须将代码设计得让人类读得懂,而不仅仅是让编译器读懂。 许多简单的策略读可以提高程序的可读性。 (1)代码语句的缩进——清楚每个句法单元的主体包含哪些行; (2)使用富有含义的名称命名变量、方法和类; 甚至像在每个运算符两边加一个空格这样简单的方法都可以极大地增加代码的可读性。 严格遵守一些简单的句法规则(正确缩排,选择合适名称),代码本身就足以表达你的想法。如...
阅读全文
摘要:1 /* 2 * File:Rainbow.java 3 * --------------- 4 * This program draws a rainbow 5 */ 6 import acm.graphics.*; 7 import acm.program.*; 8 import java.awt.*; 9 public class Rainbow extends GraphicsProgram {10 11 public void run(){12 /*blackground*/13 GRect rect = new GRect(...
阅读全文