SlideShare uma empresa Scribd logo
1 de 27
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

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Operators in java
Operators in javaOperators in java
Operators in java
 
Java Object Oriented Programming
Java Object Oriented Programming Java Object Oriented Programming
Java Object Oriented Programming
 
Fundamentals of JAVA
Fundamentals of JAVAFundamentals of JAVA
Fundamentals of JAVA
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
File handling & regular expressions in python programming
File handling & regular expressions in python programmingFile handling & regular expressions in python programming
File handling & regular expressions in python programming
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Java I/O
Java I/OJava I/O
Java I/O
 
Arrays in Java | Edureka
Arrays in Java | EdurekaArrays in Java | Edureka
Arrays in Java | Edureka
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
07 java collection
07 java collection07 java collection
07 java collection
 
Python unit 3 and Unit 4
Python unit 3 and Unit 4Python unit 3 and Unit 4
Python unit 3 and Unit 4
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
List in java
List in javaList in java
List in java
 
File handling
File handlingFile handling
File handling
 

Destaque

Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5
Bianca Teşilă
 
Java data structures for principled programmer
Java data structures for principled programmerJava data structures for principled programmer
Java data structures for principled programmer
spnr15z
 

Destaque (20)

Java 102 intro to object-oriented programming in java - exercises
Java 102   intro to object-oriented programming in java - exercisesJava 102   intro to object-oriented programming in java - exercises
Java 102 intro to object-oriented programming in java - exercises
 
Programming exercises
Programming exercisesProgramming exercises
Programming exercises
 
Introduction to Agile
Introduction to AgileIntroduction to Agile
Introduction to Agile
 
Java 101 Intro to Java Programming
Java 101 Intro to Java ProgrammingJava 101 Intro to Java Programming
Java 101 Intro to Java Programming
 
Java 201 Intro to Test Driven Development in Java
Java 201   Intro to Test Driven Development in JavaJava 201   Intro to Test Driven Development in Java
Java 201 Intro to Test Driven Development in Java
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
 
Computer Programming Overview
Computer Programming OverviewComputer Programming Overview
Computer Programming Overview
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
Java 101 intro to programming with java
Java 101  intro to programming with javaJava 101  intro to programming with java
Java 101 intro to programming with java
 
Java Day-7
Java Day-7Java Day-7
Java Day-7
 
Enum Report
Enum ReportEnum Report
Enum Report
 
Java Day-2
Java Day-2Java Day-2
Java Day-2
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
 
Java package
Java packageJava package
Java package
 
Java data structures for principled programmer
Java data structures for principled programmerJava data structures for principled programmer
Java data structures for principled programmer
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
2310 b 09
2310 b 092310 b 09
2310 b 09
 
Nosql availability & integrity
Nosql availability & integrityNosql availability & integrity
Nosql availability & integrity
 
Forms authentication
Forms authenticationForms authentication
Forms authentication
 

Semelhante a Java 101 Intro to Java Programming - Exercises

Better code, littler classes
Better code, littler classesBetter code, littler classes
Better code, littler classes
drewz lin
 
Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012
Anton Arhipov
 

Semelhante a Java 101 Intro to Java Programming - Exercises (20)

Introduction to java 101
Introduction to java 101Introduction to java 101
Introduction to java 101
 
Java 101
Java 101Java 101
Java 101
 
Java Intro: Unit1. Hello World
Java Intro: Unit1. Hello WorldJava Intro: Unit1. Hello World
Java Intro: Unit1. Hello World
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Introduction to java technology
Introduction to java technologyIntroduction to java technology
Introduction to java technology
 
Java practical(baca sem v)
Java practical(baca sem v)Java practical(baca sem v)
Java practical(baca sem v)
 
Java lab-manual
Java lab-manualJava lab-manual
Java lab-manual
 
How to write a simple java program in 10 steps
How to write a simple java program in 10 stepsHow to write a simple java program in 10 steps
How to write a simple java program in 10 steps
 
itft-Overview of java language
itft-Overview of java languageitft-Overview of java language
itft-Overview of java language
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operator
 
Pj01 2-install java and write first java program
Pj01 2-install java and write first java programPj01 2-install java and write first java program
Pj01 2-install java and write first java program
 
Java assgnmt2.
Java assgnmt2.Java assgnmt2.
Java assgnmt2.
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Better code, littler classes
Better code, littler classesBetter code, littler classes
Better code, littler classes
 
Android programming-basics
Android programming-basicsAndroid programming-basics
Android programming-basics
 
Nashorn
NashornNashorn
Nashorn
 
Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012
 
02-PF-211-Environment-2022.pptx
02-PF-211-Environment-2022.pptx02-PF-211-Environment-2022.pptx
02-PF-211-Environment-2022.pptx
 
Java
JavaJava
Java
 
Basic online java course - Brainsmartlabs
Basic online java course  - BrainsmartlabsBasic online java course  - Brainsmartlabs
Basic online java course - Brainsmartlabs
 

Último

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 

Java 101 Intro to Java Programming - Exercises