java第一章到第四章
class HelloWorld{
public static void main(String [] arguments) {
System.out.println("Hello World!");
System.out.println("Test Successly!");
}
}
简单的java测试
class MyFirstApp {
public static void main (String[] args) {
System.out.println("I Rule");
System.out.println("The World");
}
}
class Loopy {
public static void main (String[] args) {
int x=1;
System.out.println("Before the Loop");
while(x < 4) {
System.out.println("In the Loop");
System.out.println("Value of x is " + x);
x = x + 1;
}
System.out.println("This is after the Loop");
}
}
简单if语句的测试
class IfTest {
public static void main (String [] args) {
int x = 3;
if(x==3) {
System.out.println("x must be 3");
}
System.out.println("This runs no matter what");
}
}
class IfTest2 {
public static void main (String[] args) {
int x = 2;
if (x == 3) {
System.out.println ("x must be 3");
} else {
System.out.println("x is NOT 3");
}
System.out.println("This runs no matter what");
}
}
class PhraseCMatic {
public static void main(String[] args) {
String[] wordListOne = {"24/7", "multiTier", "30,000", "B-to-B", "win-win", "front-end", "web-based", "pervasive", "smart", "six-sigma", "critical-path", "dynamic"};
String[] wordListTwo = {"empoweres", "sticky", "value-added", "oriented", "centric", "distributed", "clustered", "branded", "outside-the-box", "positioned", "networked", "focused", "leveraged", "aligned", "targeted", "shared", "cooperatived", "accelerated"};
String[] wordListThree = {"process", "tipping", "point", "solution", "architecture", "core competency", "strategy", "mindshare", "portal", "space", "vision", "paradigm", "mission"};
int oneLength = wordListOne.length;
int twoLength = wordListTwo.length;
int threeLength = wordListThree.length;
int rand1 = (int) (Math.random() * oneLength);
int rand2 = (int) (Math.random() * twoLength);
int rand3 = (int) (Math.random() * threeLength);
String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];
System.out.println("What we need is a " + phrase);
}
}
class BeerSong {
public static void main(String[] args) {
int beerNum = 99;
String word = "bottles";
while(beerNum > 0) {
if(beerNum == 1) {
word = "bottle";
}
System.out.println(beerNum + " " + word + " of beer on the wall");
System.out.println(beerNum + " " + word + " of beer.");
System.out.println("Take one down");
System.out.println("Pass it around");
beerNum = beerNum - 1;
// if(beerNum > 0)
// System.out.println(beerNum + " " + word + " of beer on the wall");
// else {
// System.out.println("No more bottles of beer on the wall");
// }
if(beerNum <= 0)
{
System.out.println("No more bottles of beer on the wall" );
}
}
}
}
class Shuffel {
public static void main(String[] args) {
int x = 3;
while (x > 0) {
if(x == 1) {
System.out.print("d");
x = x - 1;
}
if(x == 2) {
System.out.print("b c");
}
if(x > 2) {
System.out.print("a");
}
x = x - 1;
System.out.print("-");
}
}
}
class Test {
public static void main(String[] args) {
int x = 0;
int y = 0;
while (x < 5) {
y = x - y;
y = y + x;
System.out.print(x + " " + y + " ");
x = x + 1;
}
}
}
class PoolPuzzleOne {
public static void main (String[] args) {
int x = 0;
while (x < 4) {
System.out.print("a");
if(x < 1) {
System.out.print(" ");
}
System.out.print("n");
if(x > 1) {
System.out.print("oyster");
x = x + 2;
}
if(x == 1) {
System.out.print("noys");
}
if(x < 1) {
System.out.print("oise");
}
System.out.println(" ");
x = x + 1;
}
}
}
class Dog {
int size;
String breed;
String name;
void bark() {
System.out.println("Ruff! Ruff!");
}
}
class DogTestDrive {
public static void main(String[] args) {
Dog d = new Dog();
d.size = 40;
d.bark();
}
}
class Movie {
String title;
String genre;
int rating;
void playIt() {
System.out.println("Playing the movie");
}
}
class MovieTestDrive {
public static void main (String[] aargs) {
Movie one = new Movie();
one.title = "Gone with the Stock";
one.rating = -2;
Movie two = new Movie();
two.genre = "Comedy";
two.rating = 5;
two.playIt();
Movie three = new Movie();
three.title = "Byte Club";
three.genre = "Tragic but ultimately uplifting";
three.rating = 127;
}
}
// 猜字游戏
class GuessGame {
Player p1;
Player p2;
Player p3;
public void startGame() {
p1 = new Player();
p2 = new Player();
p3 = new Player();
int guessp1 = 0;
int guessp2 = 0;
int guessp3 = 0;
boolean p1isRight = false;
boolean p2isRight = false;
boolean p3isRight = false;
int targetNumber = (int) (Math.random() * 10);
System.out.println("I'm thinging of a number between o and 9...");
while(true) {
System.out.println("Number to guess is " + targetNumber);
p1.guess();
p2.guess();
p3.guess();
guessp1 = p1.number;
System.out.println("Flayer one guessed " + guessp1);
guessp2 = p2.number;
System.out.println("Flayer two guessed " + guessp2);
guessp3 = p3.number;
System.out.println("Flayer three guessed " + guessp3);
if(guessp1 == targetNumber) {
p1isRight = true;
}
if(guessp2 == targetNumber) {
p2isRight = true;
}
if(guessp3 == targetNumber) {
p3isRight = true;
}
if(p1isRight || p2isRight || p3isRight) {
System.out.println("We have a winner!");
System.out.println("Player one got it right? " + p1isRight);
System.out.println("Player two got it right? " + p2isRight);
System.out.println("Player three got it right? " + p3isRight);
System.out.println("Game is over.");
break;
}
else {
System.out.println("Player will have to try again.");
}
}
}
}
class Player {
int number = 0;
public void guess() {
number = (int) (Math.random() * 10);
System.out.println("I'm guessing " + number);
}
}
class GameLauncher {
public static void main(String[] args) {
GuessGame game = new GuessGame();
game.startGame();
}
}
class DrumKit {
boolean topHat = true;
boolean snare = true;
void playTopHat() {
System.out.println("ding ding da-ding");
}
void playSnare() {
System.out.println("bang bang ba-bang");
}
}
class DrumKitTestDrive {
public static void main(String[] args) {
DrumKit d = new DrumKit();
d.playSnare();
d.snare = false;
d.playTopHat();
if(d.snare == true) {
d.playSnare();
}
}
}
class TapeDeck {
boolean canRecord = false;
void playTape() {
System.out.println("tape playing");
}
void recordTape() {
System.out.println("tape recording");
}
}
class TapeDeckTestDrive {
public static void main(String[] args) {
TapeDeck t = new TapeDeck();
t.canRecord = true;
t.playTape();
if(t.canRecord == true) {
t.recordTape();
}
}
}
class DVDPlayer {
boolean canRecord = false;
void recordDVD() {
System.out.println("DVD recording");
}
void playDVD() {
System.out.println("DVD playing");
}
}
class DVDPlayerTestDrive {
public static void main(String[] args) {
DVDPlayer d = new DVDPlayer();
d.canRecord = true;
d.playDVD();
if(d.canRecord == true) {
d.recordDVD();
}
}
}
class Echo {
int count = 0;
void hello() {
System.out.println("helloooo...");
}
}
class EchoTestDrive {
public static void main(String[] args) {
Echo e1 = new Echo();
Echo e2 = new Echo();
int x = 0;
while(x < 4) {
e1.hello();
e1.count = e1.count + 1;
if(x == 3) {
e2.count = e2.count + 1;
}
if(x > 0) {
e2.count = e2.count + e1.count;
}
x = x + 1;
}
System.out.println(e2.count);
}
}
class Dog {
String name;
public static void main (String[] args) {
Dog dog1 = new Dog();
dog1.bark();
dog1.name = "Bart";
Dog[] myDogs = new Dog[3];
myDogs[0] = new Dog();
myDogs[1] = new Dog();
myDogs[2] = dog1;
myDogs[0].name = "Fred";
myDogs[1].name = "Marge";
System.out.print("last dog's name is ");
System.out.println(myDogs[2].name);
int x = 0;
while(x < myDogs.length) {
myDogs[x].bark();
x = x + 1;
}
}
public void bark() {
System.out.println(name + " says Ruff!");
}
public void eat() {
}
public void chaseCat() {
}
}
class TestArrays {
public static void main(String[] args) {
int [] index = new int[4];
index[0] = 1;
index[1] = 3;
index[2] = 0;
index[3] = 2;
String[] islands = new String[4];
islands[0] = "Bermuda";
islands[1] = "Fiji";
islands[2] = "Azores";
islands[3] = "Cozumel";
int y = 0;
int ref;
while(y < 4) {
ref = index[y];
System.out.print("island = ");
System.out.println(islands[ref]);
y = y + 1;
}
}
}
class Books {
String title;
String author;
}
class BooksTestDrive {
public static void main (String[] args) {
Books [] myBooks = new Books[3];
int x = 0;
myBooks[0] = new Books();
myBooks[1] = new Books();
myBooks[2] = new Books();
myBooks[0].title = "The Grapes of java ";
myBooks[1].title = "The Java Gatsby ";
myBooks[2].title = "The Java Cookbook ";
myBooks[0].author = "bob";
myBooks[1].author = "sue";
myBooks[2].author = "lan";
while(x < 3) {
System.out.print(myBooks[x].title);
System.out.print(" by ");
System.out.println(myBooks[x].author);
x = x + 1;
}
}
}
class Hobbits {
String name;
public static void main(String[] args) {
Hobbits[] h = new Hobbits[3];
int z = -1;
while(z < 2) {
z = z + 1;
h[z] = new Hobbits();
h[z].name = "bilbo";
if(z == 1) {
h[z].name = "frodo";
}
if(z == 2) {
h[z].name = "sam";
}
System.out.print(h[z].name + " is a ");
System.out.println("good hobbit name");
}
}
}
class Triangle {
double area;
int height;
int length;
public static void main(String[] args) {
int x = 0;
Triangle [] ta = new Triangle[4];
while(x < 4) {
ta[x] = new Triangle();
ta[x].height = (x + 1) * 2;
ta[x].length = x + 4;
ta[x].setArea();
System.out.print("triangle " + x + ", area ");
System.out.println(" = " + ta[x].area);
x = x + 1;
}
int y = x;
x = 27;
Triangle t5 = ta[2];
ta[2].area = 343;
System.out.print("y = " + y);
System.out.println(", t5 area = " + t5.area);
}
void setArea() {
area = (height * length) / 2;
}
}
class Dog {
int size;
String name;
void bark() {
if(size > 60) {
System.out.println("Wooof! Wooof!");
} else if(size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
}
class DogTestDrive {
public static void main(String[] args) {
Dog one = new Dog();
one.size = 70;
Dog two = new Dog();
two.size = 8;
Dog three = new Dog();
three.size = 35;
one.bark();
two.bark();
three.bark();
}
}
class GoodDog {
private int size;
public int getSize() {
return size;
}
public void setSize(int s) {
size = s;
}
void bark() {
if(size > 60) {
System.out.println("Wooof! Wooof!");
} else if(size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
}
class GoodDogTestDrive {
public static void main(String[] args) {
GoodDog one = new GoodDog();
one.setSize(70);
GoodDog two = new GoodDog();
two.setSize(8);
System.out.println("Dog one : " + one.getSize());
System.out.println("Dog two : " + two.getSize());
one.bark();
two.bark();
}
}
class PoorDog {
private int size;
private String name;
public int getSize() {
return size;
}
public String getName() {
return name;
}
}
class PoorDogTestDrive {
public static void main(String[] args) {
PoorDog one = new PoorDog();
System.out.println("Dog size is " + one.getSize());
System.out.println("Dog name is " + one.getName());
}
}
class Clock {
String time;
void setTime (String t) {
time = t;
}
String getTime() {
return time;
}
}
class ClockTestDrive {
public static void main(String[] args) {
Clock c = new Clock();
c.setTime("1245");
String tod = c.getTime();
System.out.println("time : " + tod);
}
}
class Puzzle4 {
public static void main(String[] args) {
Puzzle4b [] obs = new Puzzle4b[6];
int y = 1;
int x = 0;
int result = 0;
while (x < 6) {
obs[x] = new Puzzle4b();
obs[x].ivar = y;
y = y * 10;
x = x + 1;
}
x = 6;
while(x > 0) {
x = x - 1;
result = result + obs[x].doStuff(x);
}
System.out.println("result " + result);
}
}
class Puzzle4b {
int ivar;
public int doStuff(int factor) {
if(ivar > 100) {
return ivar * factor;
} else {
return ivar * (5 - factor);
}
}
}
浙公网安备 33010602011771号