SlideShare a Scribd company logo
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: javac
HelloWorld.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 DayPrinter
Sunday
Monday
Tuesday
Wednesday
Thuesday
Friday
Saturday
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 Johnson
23 Main Street
New 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

More Related Content

What's hot (20)

PPTX
An Introduction to Maven
Vadym Lotar
 
PDF
Spring Framework - AOP
Dzmitry Naskou
 
PDF
Spring boot introduction
Rasheed Waraich
 
PDF
Introduction to Java Programming Language
jaimefrozr
 
PPTX
Spring boot Introduction
Jeevesh Pandey
 
PDF
Spring Security
Knoldus Inc.
 
PPTX
Introduction Node.js
Erik van Appeldoorn
 
ODP
Introduction To Java.
Tushar Chauhan
 
PPTX
Spring boot
sdeeg
 
PDF
javathreads
Arjun Shanka
 
PPTX
L21 io streams
teach4uin
 
PPTX
Introduction to OOP(in java) BY Govind Singh
prabhat engineering college
 
PPTX
Basic Concept of Node.js & NPM
Bhargav Anadkat
 
PDF
Introduction to Java Programming
Ravi Kant Sahu
 
PPTX
Core java complete ppt(note)
arvind pandey
 
PPSX
Elements of Java Language
Hitesh-Java
 
PDF
Java Thread Synchronization
Benj Del Mundo
 
PPTX
Spring Boot
Jiayun Zhou
 
PPTX
Java 8 Date-Time API
Anindya Bandopadhyay
 
PDF
Introduction to Node.js
Rob O'Doherty
 
An Introduction to Maven
Vadym Lotar
 
Spring Framework - AOP
Dzmitry Naskou
 
Spring boot introduction
Rasheed Waraich
 
Introduction to Java Programming Language
jaimefrozr
 
Spring boot Introduction
Jeevesh Pandey
 
Spring Security
Knoldus Inc.
 
Introduction Node.js
Erik van Appeldoorn
 
Introduction To Java.
Tushar Chauhan
 
Spring boot
sdeeg
 
javathreads
Arjun Shanka
 
L21 io streams
teach4uin
 
Introduction to OOP(in java) BY Govind Singh
prabhat engineering college
 
Basic Concept of Node.js & NPM
Bhargav Anadkat
 
Introduction to Java Programming
Ravi Kant Sahu
 
Core java complete ppt(note)
arvind pandey
 
Elements of Java Language
Hitesh-Java
 
Java Thread Synchronization
Benj Del Mundo
 
Spring Boot
Jiayun Zhou
 
Java 8 Date-Time API
Anindya Bandopadhyay
 
Introduction to Node.js
Rob O'Doherty
 

Viewers also liked (20)

PPTX
Java 102 intro to object-oriented programming in java - exercises
agorolabs
 
PDF
Programming exercises
Terry Yin
 
PDF
Introduction to Agile
agorolabs
 
PPTX
Java 101 Intro to Java Programming
agorolabs
 
PPTX
Java 201 Intro to Test Driven Development in Java
agorolabs
 
PPTX
Java 102 intro to object-oriented programming in java
agorolabs
 
PPTX
Computer Programming Overview
agorolabs
 
PPTX
Java 103 intro to java data structures
agorolabs
 
PPTX
Java 101 intro to programming with java
Hawkman Academy
 
PDF
Java Day-7
People Strategists
 
PDF
Enum Report
enumplatform
 
PDF
Java Day-2
People Strategists
 
PPTX
Data structures and algorithms lab5
Bianca Teşilă
 
PPSX
data structure(tree operations)
Waheed Khalid
 
PPTX
Java package
CS_GDRCST
 
PDF
Java data structures for principled programmer
spnr15z
 
PPT
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
PPT
2310 b 09
Krazy Koder
 
ODP
Nosql availability & integrity
Fahri Firdausillah
 
PPT
Forms authentication
SNJ Chaudhary
 
Java 102 intro to object-oriented programming in java - exercises
agorolabs
 
Programming exercises
Terry Yin
 
Introduction to Agile
agorolabs
 
Java 101 Intro to Java Programming
agorolabs
 
Java 201 Intro to Test Driven Development in Java
agorolabs
 
Java 102 intro to object-oriented programming in java
agorolabs
 
Computer Programming Overview
agorolabs
 
Java 103 intro to java data structures
agorolabs
 
Java 101 intro to programming with java
Hawkman Academy
 
Java Day-7
People Strategists
 
Enum Report
enumplatform
 
Java Day-2
People Strategists
 
Data structures and algorithms lab5
Bianca Teşilă
 
data structure(tree operations)
Waheed Khalid
 
Java package
CS_GDRCST
 
Java data structures for principled programmer
spnr15z
 
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
2310 b 09
Krazy Koder
 
Nosql availability & integrity
Fahri Firdausillah
 
Forms authentication
SNJ Chaudhary
 
Ad

Similar to Java 101 Intro to Java Programming - Exercises (20)

PPTX
Java 101
Manuela Grindei
 
PPTX
Introduction to java 101
kankemwa Ishaku
 
PDF
Java Lab
Leo Nguyen
 
PDF
Proyect of english
Carlos Alcivar
 
PDF
Java for beginners programming course
Marius Claassen
 
PDF
Java for beginners programming course (updated)
Marius Claassen
 
PDF
Java conceptual learning material
ArthyR3
 
PDF
Java Script Sysllabus for the computer.pdf
JotiramShinde4
 
PPTX
Fundamentals of java --- version 2
Uday Sharma
 
PPTX
Java-Programming.forBSITSTUDENTfreespptx
oliverrobertjames
 
PPTX
Core Java introduction | Basics | free course
Kernel Training
 
PPT
bai giang java co ban - java cơ bản - bai 1
ifis
 
PDF
OpenThink Labs Training : Diving into Java, The Head First Way
Wildan Maulana
 
PDF
Download full ebook of Programming With Java Edet Theophilus instant download...
noyzdapat39
 
PDF
Intro To Java Alpharetta Meetup Day-1
introtojava
 
PDF
Java 8 for complete beginners
Marius Claassen
 
PDF
03 expressions.ppt
Business man
 
PDF
Java for complete beginners programming course
Marius Claassen
 
Java 101
Manuela Grindei
 
Introduction to java 101
kankemwa Ishaku
 
Java Lab
Leo Nguyen
 
Proyect of english
Carlos Alcivar
 
Java for beginners programming course
Marius Claassen
 
Java for beginners programming course (updated)
Marius Claassen
 
Java conceptual learning material
ArthyR3
 
Java Script Sysllabus for the computer.pdf
JotiramShinde4
 
Fundamentals of java --- version 2
Uday Sharma
 
Java-Programming.forBSITSTUDENTfreespptx
oliverrobertjames
 
Core Java introduction | Basics | free course
Kernel Training
 
bai giang java co ban - java cơ bản - bai 1
ifis
 
OpenThink Labs Training : Diving into Java, The Head First Way
Wildan Maulana
 
Download full ebook of Programming With Java Edet Theophilus instant download...
noyzdapat39
 
Intro To Java Alpharetta Meetup Day-1
introtojava
 
Java 8 for complete beginners
Marius Claassen
 
03 expressions.ppt
Business man
 
Java for complete beginners programming course
Marius Claassen
 
Ad

Recently uploaded (20)

PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Human Resources Information System (HRIS)
Amity University, Patna
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 

Java 101 Intro to Java Programming - Exercises