Chapter 1

9
Chapter 1 Exercise 1 Sol Its about Creating File, writing a program, saving, compiling and running it. Exercise 2 Sol package practice; public class Exercise2 { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println("I am learning to program in java."); System.out.println("I hope it is a lot of fun!"); System.out.println(); System.out.println("I hope I get a good grade!"); System.out.println(); System.out.println("Maybe I'll change my major to computer science."); } } Exercise 3 Sol

description

chapter 1

Transcript of Chapter 1

Page 1: Chapter 1

Chapter 1

Exercise 1

Sol

Its about Creating File, writing a program, saving, compiling and running it.

Exercise 2

Sol

package practice;

public class Exercise2 {

public static void main(String[] args) {

System.out.println("Hello, world!");

System.out.println("I am learning to program in java.");

System.out.println("I hope it is a lot of fun!");

System.out.println();

System.out.println("I hope I get a good grade!");

System.out.println();

System.out.println("Maybe I'll change my major to computer science.");

}

}

Exercise 3

Sol

Discover what error messages the compiler produces when you make each of the following mistakes. How many unique error messages are you able to cause the compiler to produce?

Naming your file incorrectly, then compiling.

The public type Exercise must be defined in its own file

Page 2: Chapter 1

Forgetting a keyword such as void or class

Return type for the method is missing

Forgetting a quotation mark "

Syntax error on token(s), misplaced construct(s)

Forgetting a parenthesis ( or )

Syntax error, insert ")" to complete Expression

Forgetting a dot .

The method outprintln(String) is undefined for the type System

Using too many or too few braces { or }

Syntax error, insert "}" to complete ClassBody

Exercise 4

Sol

If last line is counted then it will be 7 lines, otherwise 6 lines.

Exercise 5

Sol

Shaq is 7'1

The string "" is an empty message.

\'"\\"

Exercise 6

Sol

package practice;

public class MuchBetterExcercise6 {

public static void main(String[] args) {

System.out.println("A \"quoted\" String is");

Page 3: Chapter 1

System.out.println("'much' better if you learn");

System.out.println("the rules of \"escape sequences.\"");

System.out.println();

System.out.println("Also, \"\" represents an empty String.");

System.out.println("Don't forget: use \\\" instead of \" !");

System.out.println("'' is not the same as \"");

}

}

Exercise 7

Sol

package practice;

public class SpykeyExcercise7 {

public static void main(String[] args) {

System.out.println(" \\/");

System.out.println(" \\\\//");

System.out.println("\\\\\\///");

System.out.println("///\\\\\\");

System.out.println(" //\\\\");

System.out.println(" /\\");

}

}

Exercise 8

Page 4: Chapter 1

Sol

public class Icky {

public static void main(String[] args) {

System.out.println("Well-indented programs");

System.out.println("look much better.");

System.out.println("Please fix me");

System.out.println("so I look nicer");

}

}

Exercise 9

Sol

package practice;

public class Excercise9 {

public static void main(String[] args) {

System.out.println("Hello world");

System.out.println("Do you like this program?");

System.out.println();

System.out.println("I wrote it myself.");

}

}

Page 5: Chapter 1

Static method

Exercise 1

Sol

package practice;

public class SpykeyExcercise7 {

public static void main(String[] args) {

System.out.println(" \\/");

System.out.println(" \\\\//");

System.out.println("\\\\\\///");

System.out.println("///\\\\\\");

System.out.println(" //\\\\");

System.out.println(" /\\");

}

}

Exercise 2

Sol

package practice;

public class LanternsStaticExercise2 {

public static void main(String[] ags) {

triangle();

triangle();

Page 6: Chapter 1

StarLine();

star_13();

triangle();

triangle();

star_5();

StarLine();

StarLine();

star_5();

star_5();

}

public static void star_5() {

System.out.println(" ***** ");

}

public static void star_13() {

System.out.println("*************");

}

Page 7: Chapter 1

public static void StarLine() {

System.out.println("* | | | | | *");

}

public static void triangle() {

System.out.println(" ***** ");

System.out.println(" ********* ");

System.out.println("*************");

}

}

Exercise 3

Sol

package practice;

public class GaintLettersExcercise3 {public static void main(String[] args) {

N();I();R();A();N();

}

public static void A() {System.out.println("AAAAAAAAAA");System.out.println("A A");System.out.println("A A");System.out.println("AAAAAAAAAA");System.out.println("A A");System.out.println("A A");

}

public static void R() {

Page 8: Chapter 1

System.out.println("RRRRRRRRRR");System.out.println("R R");System.out.println("RRRRRRRRRR");System.out.println("R R ");System.out.println("R R ");System.out.println("R R");

}

public static void I() {System.out.println("IIIIIIIIII");System.out.println(" I ");System.out.println(" I ");System.out.println(" I ");System.out.println(" I ");System.out.println("IIIIIIIIII");

}

public static void N() {System.out.println("NN N");System.out.println("N N N");System.out.println("N N N");System.out.println("N N N");System.out.println("N N N");System.out.println("N NN");

}

}

Exercise 4

Sol

package practice;

public class FightSongExercise4 {public static void main(String[] args) {

Goteam();Best();Goteam();Best();Goteam();

}

public static void Best() {System.out.println("You're the best,");System.out.println("In the West.");

}

public static void Goteam() {System.out.println("Go, team, go!");

Page 9: Chapter 1

System.out.println("You can do it.\n");System.out.println("Go, team, go!");System.out.println("You can do it.");

}

}