Head First Java_Study Notes_Chapter 1 Break the Surface

来源:互联网 发布:mysql 最近3天 编辑:程序博客网 时间:2024/06/06 17:28

Chapter 1 Break the Surface

1.Code structure in Java:

Put a class in a source file:

private class Dog{}


Put methods in a class:
private class Dog{void Bark (){}}


Put statements in a method:
private class Dog{void Bark (){statements1;statements2;}}

2.Anatomy of a Class

The Java Virtural Machine(JVM) starts with the main method:

public static void main (String[] args) { // your code goes here}

Each statement must end in a semicolon.
x = x + 1;


3.Looping

Java has three standard looping constructs:while,do-while, andfor. The key to a loop is the conditional test, an expression that results in a boolean value—in other words, something that is eithertrue orfalse.

Practice:

Given the output:

% java DooBee 

DooBeeDooBeeDo

Fill in the missing code:

public class DooBee {
public static void main (String[] args) {
int x = 1;
while (x < 3) {
System.out._____(“Doo”); 
System.out._____(“Bee”); 
x = x + 1;
}
if(x==__ ){
System.out.print(“Do”); }

}

My answer:


public class DooBee {public static void main (String[] args) {int x = 1;while (x < 3) {System.out.print("Doo"); System.out.print("Bee"); x = x + 1;}if(x==3){System.out.print("Do"); }} }

Coding a Serious Business Application: "99 bottles of beer"

Additional information (from Wikipedia): "99 Bottles of Beer" is an anonymous United States folk song dating to the mid-20th century. It is a traditional song in both the United States and Canada. It is popular to sing on long trips, as it has a very repetitive format which is easy to memorize, and can take a long time to sing. In particular the song is frequently sung by children on long bus trips, such as class field trips, or on Scout and/or Girl Guide outings. The song is derived from the English "Ten Green Bottles".

The song's lyrics are as follows:

99 bottles of beer on the wall, 99 bottles of beer.
Take one down, pass it around, 98 bottles of beer on the wall...
Alternate line: If one of those bottles should happen to fall, 98 bottles of beer on the wall...

The same verse is repeated, each time with one bottle less. The song is completed when the singer or singers reach zero. Variations on the last verse following the last bottle going down include lines such as "No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall." and simply "If that one bottle should happen to fall, what a waste of alcohol!". It takes approximately 20 to 30 minutes to sing all 100 verses.

Sometimes, the word "beer" is replaced by the word "Coke" or "milk" when being sung by groups (such as religious groups) that consider the consumption of alcohol to be inappropriate.

Below is the original code on the book:

public 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");}}}}

But this code is not 100% perfect. So I made a little change as below:

public class BeerSong {public static void main (String[] args){int beerNum = 99;String word = "bottles";while (beerNum > 0){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 == 1){word = "bottle";}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");}}}}





0 0