3. Create Basic Java Program
• Create the following program by typing it into
a text editor, and save it as HelloWorld.java.
4. Compile Basic Java Program
• Compile it by typing at the command-line: javac
HelloWorld.java.
• This creates a Java bytecode file named:
HelloWorld.class.
5. Execute Basic Java Program
• Execute it by typing at the command-line: java
HelloWorld.
9. 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.
12. 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
15. 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
19. 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
22. 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.
23. 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 DayPrinter
Sunday
Monday
Tuesday
Wednesday
Thuesday
Friday
Saturday
25. Exercise: Print Personal Details
• Write a program that will print your name and
address in the Console view of Eclipse, for
example:
Alex Johnson
23 Main Street
New York, NY 10001 USA
27. Exercise: Sales Discount
• Create a new project in Eclipse named Sale
• Create, compile, and run the FriendsAndFamily class as illustrated below
• Debug this program in your IDE to find out how it works