Download CSE114 Spring 2016 Lab Exercise 1 of Week 1 Hello World Java!

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSE114 Spring 2016
Lab Exercise 1 of Week 1
Hello World Java!
Chen-Wei Wang
1
Installing Java and Eclipse
– Download and install Java SE Development Kit Version 8 :
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
– Download the Eclipse IDE (Integrated Development Environment):
http://www.eclipse.org/downloads/
– Choose Eclipse IDE for Java Developers to download.
– Unzip the downloaded zip file to somewhere convenient (typically C://Program Files).
– As you wish, create a shortcut to the Eclipse program contained in the unzipped folder.
– It is important that either you install the 32-bit versions for both software, or the 64-bit
versions for both, but not install mixed versions.
2
Launching Eclipse
– Double-click the Eclipse shortcut you created from the previous step.
– You may create a folder workspace that will store all your Java projects for this course.
– Select the workspace that you created, or just go with the default (typically C://Documents/workspace).
3
Exercises
1. Create a new project:CSE114 S16 Week01 Lab1
2. Create a new class HelloWorld in the project:
public class HelloWorld {
public static void main(Strng[] args) {
/* Print a greeting message. */
System.out.println("Hello World!");
}
}
3. Execute the above Java program (Right click on the class and click on Run as Java application.)
4. Modify the above program so that it outputs a different greeting message (e.g., your name).
5. Create another new class HelloWorld2 in the project:
1
import java.util.Scanner;
public class HelloWorld2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a name to greet:");
String name = input.nextLine();
System.out.println("Hello " + name + "!");
}
}
A sample run:
Enter a name to greet:
Jackie
Hello Jackie!
6. Create another new class HelloWorld3 in the project. Adapt the above program so that it prompts
for two names, one name at a time, and greets the two persons together.
import java.util.Scanner;
public class HelloWorld3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the first name to greet:");
String name1 = input.nextLine();
System.out.println("Enter the second name to greet:");
String name2 = input.nextLine();
System.out.println("Hello " + name1 + " and " + name2 + "!");
}
}
A sample run:
Enter the first name to greet:
Jackie
Enter the second name to greet:
Jim
Hello Jackie and Jim!
7. Create another new class HelloWorld4 in the project. Adapt the above program so that it prompts
for a first name, greets it, prompts for a second name, and greets it.
2
import java.util.Scanner;
public class HelloWorld4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the first name to greet:");
String name1 = input.nextLine();
System.out.println("Hello " + name1 + "!");
System.out.println("Enter the second name to greet:");
String name2 = input.nextLine();
System.out.println("Hello " + name2 + "!");
}
}
A sample run:
Enter the first name to greet:
Jackie
Hello Jackie!
Enter the second name to greet:
Jim
Hello Jim!
3