+ All Categories
Home > Software > Java 101 Intro to Java Programming - Exercises

Java 101 Intro to Java Programming - Exercises

Date post: 17-Jul-2015
Category:
Upload: agorolabs
View: 398 times
Download: 13 times
Share this document with a friend
Popular Tags:
27
Hands-on Exercises Java 101: Introduction to Java
Transcript

Hands-on Exercises

Java 101: Introduction to Java

Hands-on Exercise

Basic Java Program

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.

Execute Basic Java Program

• Execute it by typing at the command-line: java HelloWorld.

Hands on Exercise

Command Line Arguments

Command Line Arguments

• Create a program that takes a name as command-line argument and prints “Hi <name>, How are you?”

Hands-on Exercise

Integer Operations

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.

Solution: Integer Operations

Hands-on Exercise

Leap Year Finder

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

Solution: Leap Year Finder

Hands-on Exercise

Powers of Two

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

Solution: Power of 2

Bonus Exercises

Java 101: Introduction to Java

Hands-on Exercise

Random Number Generator

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

Solution: Random Number Generator

Hands-on Exercise

Array of Days

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

Hands-on Exercise

Print Personal Details

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

Hands-on Exercise

Sales Discount

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


Recommended