Program Structure Package declaration Class declaration Multiline comment

  • Slides: 4
Download presentation
Program Structure Package declaration: Class declaration: Multi-line comment: package my. Hello. App; public class

Program Structure Package declaration: Class declaration: Multi-line comment: package my. Hello. App; public class Hello. World { /* this class implements a simple hello world with variables */ public static void main(String args[]) { Method declaration: String name = “John"; int age = 29; Variable declaration: // This code prints to the screen Single-line comment: System. out. println("Hello World. My name is "+name); System. out. println("My age is "+age); Program code: Close out method: Close out class: } }

Declaring Variables public class Class. Name { public static void main(String args[]) { //

Declaring Variables public class Class. Name { public static void main(String args[]) { // declare your variable name and its type variable. Type variable. Name; /* declare your variable name, its type and initialize it to a specific value */ variable. Type variable. Name = initial. Value; // if your variable is a string, it must be enclosed in double quotes String variable. Name = “initial. Value”; // print your variable System. out. println( variable. Name ); } }

Retrieving String inputs from the keyboard package Package. Name; // import the Scanner class

Retrieving String inputs from the keyboard package Package. Name; // import the Scanner class import java. util. Scanner; public class Class. Name { public static void main(String args[]) { // Declare a new input scanner for use in your program Scanner input = new Scanner (System. in); // declare a String variable String name; // Ask the user for his/her name and retrieve it from the keyboard System. out. println(“What is your name? ”); name = input. next(); // print the user’s name System. out. println(“Hello “ + name + “! Nice to meet you!”); } }

Retrieving Integer inputs from the keyboard package Package. Name; // import the Scanner class

Retrieving Integer inputs from the keyboard package Package. Name; // import the Scanner class import java. util. Scanner; public class Class. Name { public static void main(String args[]) { // Declare a new input scanner for use in your program Scanner input = new Scanner (System. in); // declare an Integer variable int age; // Ask the user for his/her age and retrieve it from the keyboard System. out. println(“How old are you? ”); age = input. next. Int(); // print the user’s age System. out. println(“”Wow! You are “ + age + “ years old! You look great!”); } }