Create Basic Java Program
• Create the following program by typing it into a text editor, and save it as HelloWorld.java.
Compile Basic Java Program
• Compile it by typing at the command-line: javacHelloWorld.java.
• This creates a Java bytecode file named: HelloWorld.class.
Command Line Arguments
• Create a program that takes a name as command-line argument and prints “Hi <name>, How are you?”
Exercise: Integer Operations
• Create new Java project in Eclipse called IntegerOperations• Create a Java class named IntOps that performs integer operations on a pair of integers
from the command line and prints the results.
Exercise: Leap Year Finder
• A year is a leap year if it is either divisible by 400 or divisible by 4 but not 100.
• Create a java project in Eclipse named LeapYearFinder
• Write a java class named LeapYear that takes a year as command line argument and prints true if it’s a leap year and false if not
Exercise: Powers of Two
• Create a new Java project in Eclipse named Pow2• Write a java class named PowerOfTwo to print powers of 2 that are
<= 2N where N is a number passed as an argument to the program.– Increment i from 0 to N. – Double v each time
Exercise: Random Number Generator
• Create a new java project in Eclipse called RandomInteger• Write a java class named RandomInt to generate a pseudo-random
number between 0 and N-1 where N is a number passed as an argument to the program
Exercise: Array of Days
• Create a new Java project in Eclipse named ArrayOfDays• Create a java class named DayPrinter that prints out
names of the days in a week from an array using a for-loop.
Solution: Arrays of Days
public class DayPrinter {
public static void main(String[] args) {
//initialize the array with the names of days of the
week
String[] daysOfTheWeek =
{"Sunday","Monday","Tuesday","Wednesday",
"Thuesday","Friday”,"Saturday"};
//loop through the array and print their elements to
//stdout
for (int i= 0;i < daysOfTheWeek.length;i++ ){
System.out.println(daysOfTheWeek[i]);
}
}
}
% javac DayPrinter.java
% java DayPrinterSundayMondayTuesdayWednesdayThuesdayFridaySaturday
Exercise: Print Personal Details
• Write a program that will print your name and address in the Console view of Eclipse, for example:
Alex Johnson23 Main StreetNew York, NY 10001 USA