Java Exercises JavaDataTypes
Java Exercises JavaDataTypes
1. Coding style:
Read Java code convention: "Google Java Style Guide" or "Oracle Java Code Con-
ventions".
Follow the Java Naming Conventions for variables, methods, and classes STRICTLY.
Use CamelCase for names. Variable and method names begin with lowercase, while
class names begin with uppercase. Use nouns for variables (e.g., radius) and class
names (e.g., Circle). Use verbs for methods (e.g., getArea(), isEmpty()).
Use Meaningful Names: Do not use names like a, b, c, d, x, x1, x2, and x1688
- they are meaningless. Avoid single-alphabet names like i, j, k. They are easy to
type, but usually meaningless. Use single-alphabet names only when their meaning
is clear, e.g., x, y, z for co-ordinates and i for array index. Use meaningful names like
row and col (instead of x and y, i and j, x1 and x2), numStudents (not n), maxGrade,
size (not n), and upperbound (not n again). Differentiate between singular and plural
nouns (e.g., use books for an array of books, and book for each item).
Use consistent indentation and coding style. Many IDEs (such as Eclipse / NetBeans)
can re-format your source codes with a single click.
3. The only way to learn programming is program, program and program on challenging
problems. The problems in this tutorial are certainly NOT challenging. There are tens of
thousands of challenging problems available – used in training for various programming
contests (such as International Collegiate Programming Contest (ICPC), International
Olympiad in Informatics (IOI)).
1
HaQT Fundamental Programming
Sample output:
Command window
1 Hello
Your Name !
Hints:
Sample solution:
p u b l i c c l a s s NamePrinter {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
System . out . p r i n t l n ( " . . . " ) ;
4 }
}
1 import j a v a . u t i l . Sc anne r ;
3 p u b l i c c l a s s PrintName {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
5 S ca nner i n p u t = new S canne r ( System . i n ) ;
13 input . c l o s e () ;
15 System . out . p r i n t l n ( ) ;
System . out . p r i n t l n ( " H e l l o \n" + fname + " " + lname ) ;
17 }
}
1.2 JavaPatternPrinter
Write a Java program to display the following pattern.
2
HaQT Fundamental Programming
Command window
J a v v a
2 J a a v v a a
J J aaaaa V V aaaaa
4 JJ a a V a a
Hints:
public c l a s s JavaPatternPrinter {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
System . out . p r i n t l n ( " J a v v a ") ;
4 System . out . p r i n t l n ( . . . ) ;
System . out . p r i n t l n ( . . . ) ;
6 System . out . p r i n t l n ( . . . ) ;
}
8 }
1.3 FacePatternPrinter
Write a Java program to print a face.
Expected output:
Command window
+"""""+
2 [| o o |]
| ^ |
4 | ’= ’ |
+- - - -=+
Hints:
3
HaQT Fundamental Programming
1 public c l a s s FacePatternPrinter {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 System . out . p r i n t l n ( " +\"\"\"\"\"+ " ) ;
...
5 }
}
1.4 Operations
Write a Java program to print the sum (addition), multiply, subtract, divide and remainder of two
numbers.
Sample output:
Command window
I n p u t f i r s t number : 125
2 I n p u t s e c o n d number : 24
125 + 24 = 149
4 125 = 24 = 101
125 x 24 = 3000
6 125 / 24 = 5
125 mod 24 = 5
Hints
1 import j a v a . u t i l . Sc anne r ;
3 public c l a s s Operations {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
5 S ca nner s c a n n e r = new Sca nner ( System . i n ) ;
System . out . p r i n t l n ( " I n p u t t h e f i r s t number : " ) ;
7 i n t number1 = s c a n n e r . n e x t I n t ( ) ;
System . out . p r i n t l n ( " I n p u t t h e s e c o n d number : " ) ;
9 i n t number2 = s c a n n e r . n e x t I n t ( ) ;
input . c l o s e () ;
11
i n t sum = . . . ;
13 System . out . p r i n t f ( . . . ) ;
15 i n t minus = . . . ;
System . out . p r i n t f ( . . . ) ;
17
int multiply = . . . ;
19 System . out . p r i n t f ( . . . ) ;
21 int subtract = . . . ;
4
HaQT Fundamental Programming
System . out . p r i n t f ( . . . ) ;
23
int divide = . . . ;
25 System . out . p r i n t f ( . . . ) ;
27 i n t remainer = . . . ;
System . out . p r i n t f ( . . . ) ;
29 }
}
1.5 ExpressionCalculator
Write a Java program to compute the specified expressions and print the output.
.
Hints:
1.6 ExpressionEvaluation
Write a Java program to find the value of specified expression.
1. (101 + 0)/3
2. 3.0e − 6 ∗ 10000000.1
5
HaQT Fundamental Programming
Expected output:
Command window
( 1 0 1 + 0 ) / 3 )=> 33
2 ( 3 . 0 e =6 * 1 0 0 0 0 0 0 0 . 1 )=> 3 0 . 0 0 0 0 0 0 3
( t r u e && t r u e )=> t r u e
4 ( f a l s e && t r u e )=> f a l s e
( ( f a l s e && f a l s e ) | | ( t r u e && t r u e ) )=> t r u e
6 ( false || f a l s e ) && ( t r u e && t r u e )=> f a l s e
Hints:
public c l a s s ExpressionEvaluation {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
i n t exp1 = . . . ;
4 d o u b l e exp2 = . . . ;
b o o l e a n exp3 = . . . ;
6 b o o l e a n exp4 = . . . ;
b o o l e a n exp5 = . . . ;
8 b o o l e a n exp6 = . . . ;
1.7 SwapVariables
Write a Java program to swap two variables.
Sample output:
Command window
1 I n p u t t h e number x : 15
I n p u t t h e number y : 25
3 B e f o r e swapping : x , y = 1 5 , 25
A f t e r swapping : x , y = 2 5 , 15
6
HaQT Fundamental Programming
Hints:
The simplest method to swap two variables is to use a third temporary variable:
import j a v a . u t i l . Sc anne r ;
2
p u b l i c c l a s s SwapVariables {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Sca nner i n p u t = new S canne r ( System . i n ) ;
6 System . out . p r i n t l n ( " I n p u t t h e number x : " ) ;
in t x = input . nextInt () ;
8 System . out . p r i n t l n ( " I n p u t t h e number y : " ) ;
in t y = input . nextInt () ;
10 System . out . p r i n t l n ( " B e f o r e swapping : x , y = " + x + " , " + y ) ;
input . c l o s e () ;
12
i n t temp = . . . ;
14 x = ...;
y = ...;
16 System . out . p r i n t l n ( " A f t e r swapping : x , y = " + x + " , " + y ) ;
}
18 }
1.8 SortNumbers
Write a program that accepts three numbers from the user and prints the numbers in increasing order.
Sample output
Command window
I n p u t t h e number x : 1
2 I n p u t t h e number y : 2
I n p u t t h e number z : 3
4 Numbers i n i n c r e a s i n g o r d e r : 1 2 3
7
HaQT Fundamental Programming
Hints:
import j a v a . u t i l . Sc anne r ;
2
p u b l i c c l a s s SortNumbers {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
S ca nner i n p u t = new S canne r ( System . i n ) ;
6 System . out . p r i n t l n ( " I n p u t t h e number x : " ) ;
in t x = input . nextInt () ;
8 System . out . p r i n t l n ( " I n p u t t h e number y : " ) ;
in t y = input . nextInt () ;
10 System . out . p r i n t l n ( " I n p u t t h e number z : " ) ;
in t z = input . nextInt () ;
12 input . c l o s e () ;
1.9 ModulesCalculator
Write a Java program to calculate the modules of two numbers without using any inbuilt modulus
operator.
Sample output:
Command window
I n p u t t h e f i r s t number : 19
2 I n p u t t h e s e c o n d number : 7
19 module 7 g i v e 5
Hints:
1 import j a v a . u t i l . * ;
3 public c l a s s ModulesCalculator {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
5 S ca nner i n p u t = new S canne r ( System . i n ) ;
System . out . p r i n t ( " I n p u t t h e f i r s t number : " ) ;
7 i n t firstNumber = input . nextInt () ;
System . out . p r i n t ( " I n p u t t h e s e c o n d number : " ) ;
9 i n t secondNumber = i n p u t . n e x t I n t ( ) ;
input . c l o s e () ;
8
HaQT Fundamental Programming
11
i n t d i v i d e d = f i r s t N u m b e r / secondNumber ;
13 int result = . . . ;
System . out . p r i n t l n ( . . . ) ;
15 }
}
1.10 TemperatureConverter
Write a Java program to convert temperature from Fahrenheit to Celsius degree.
The Fahrenheit scale is a temperature scale based on one proposed in 1724 by physicist Daniel Gabriel
Fahrenheit. It uses the degree Fahrenheit (symbol: ◦ F) as the unit.
The Celsius scale, previously known as the centigrade scale, is a temperature scale used by the Inter-
national System of Units (SI). As an SI derived unit, it is used by all countries in the world, except the
U.S.
Sample output:
Command window
I n p u t a d e g r e e i n F a h r e n h e i t : 212
2 212.0 degree Fahrenheit i s equal to 100.0 in C e l s i u s
9
HaQT Fundamental Programming
Hints:
import j a v a . u t i l . Sc anne r ;
2
p u b l i c c l a s s TemperatureC onverter {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
S ca nner i n p u t = new S canne r ( System . i n ) ;
6 System . out . p r i n t ( " I n p u t a d e g r e e i n F a h r e n h e i t : " ) ;
d o u b l e f a h r e n h e i t = i n p u t . nextDouble ( ) ;
8 input . c l o s e () ;
10 double celsius = . . . ;
System . out . p r i n t l n ( f a h r e n h e i t + " d e g r e e F a h r e n h e i t i s e q u a l t o " + c e l s i u s + " i n
,→ C e l s i u s " ) ;
12 }
}
1.11 TimeConverter
Write a Java program to convert seconds to hour, minute and seconds.
Sample output:
Command window
1 I n p u t s e c o n d s : 86399
23:59:59
10
HaQT Fundamental Programming
Hints:
import j a v a . u t i l . * ;
2
p u b l i c c l a s s TimeConverter {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
S ca nner i n p u t = new S canne r ( System . i n ) ;
6 System . out . p r i n t ( " I n p u t s e c o n d s : " ) ;
i n t numSeconds = i n p u t . n e x t I n t ( ) ;
8 input . c l o s e () ;
10 int seconds = . . . ;
i n t minute = . . . ;
12 i n t hour = . . . ;
System . out . p r i n t ( hour + " : " + minute + " : " + s e c o n d s ) ;
14 System . out . p r i n t ( " \n" ) ;
}
16 }
1.12 CircleFeaturesCalculator
Write a Java program to print the area and perimeter of a circle.
Sample output:
Command window
11
HaQT Fundamental Programming
Hints:
1 import j a v a . u t i l . Sc anne r ;
// Using Math . PI
11 double perimeter = . . .
double area = . . .
13
1.13 AreaHexagonCalculator
Write a Java program to compute the area of a hexagon.
Area of a hexagon = (6 ∗ s2 )/(4 ∗ tan(π/6)), where s is the length of a side.
Sample output:
Command window
1 I n p u t t h e l e n g t h o f a s i d e o f t h e hexagon : 6
The a r e a o f t h e hexagon i s : 9 3 . 5 3 0 7 4 3 6 0 8 7 1 9 3 8
12
HaQT Fundamental Programming
Hints:
import j a v a . u t i l . Sc anne r ;
2
p u b l i c c l a s s AreaHexagonCalculator {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
S ca nner i n p u t = new S canne r ( System . i n ) ;
6 System . out . p r i n t ( " I n p u t t h e l e n g t h o f a s i d e o f t h e hexagon : " ) ;
d o u b l e s i d e = i n p u t . nextDouble ( ) ;
8 input . c l o s e () ;
p u b l i c s t a t i c d o u b l e hexagonArea ( d o u b l e s i d e ) {
14 double area = . . .
return area ;
16 }
}
1.14 AreaPolygonCalculator
Write a Java program to compute the area of a polygon.
Area of a polygon = (n ∗ s2 )/(4 ∗ tan(π/n)), where n is n-sided polygon and s is the length of a side.
Sample output:
Command window
1 I n p u t t h e number o f s i d e s on t h e p o l y g o n : 7
I n p u t t h e l e n g t h o f one o f t h e s i d e s : 6
3 The a r e a i s : 1 3 0 . 8 2 0 8 4 7 9 8 4 0 5 7 2 2
13
HaQT Fundamental Programming
Hints:
1 import j a v a . u t i l . Sc anne r ;
3 public c l a s s AreaPolygonCalculator {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
5 S ca nner i n p u t = new S cann er ( System . i n ) ;
System . out . p r i n t ( " I n p u t t h e number o f s i d e s on t h e p o l y g o n : " ) ;
7 i n t numSides = i n p u t . n e x t I n t ( ) ;
System . out . p r i n t ( " I n p u t t h e l e n g t h o f one o f t h e s i d e s : " ) ;
9 d o u b l e s i d e = i n p u t . nextDouble ( ) ;
input . c l o s e () ;
11
15 p u b l i c s t a t i c d o u b l e polygonArea ( i n t numSides , d o u b l e s i d e ) {
double area = . . .
17 return area ;
}
19 }
1.15 DistanceTwoPointsOnSurfaceOfEarth
Write a Java program to compute the distance between two points on the surface of earth.
Distance between the two points A(x1, y1) and B(x2, y2),
14
HaQT Fundamental Programming
Sample output:
Command window
1 I n p u t t h e l a t i t u d e o f c o o r d i n a t e 1 : 25
I n p u t t h e l o n g i t u d e o f c o o r d i n a t e 1 : 35
3 Input the l a t i t u d e o f c o o r d i n a t e 2 : 35.5
Input the l o n g i t u d e o f c o o r d i n a t e 2 : 25.5
5 The d i s t a n c e between t h o s e p o i n t s i s : 1 4 8 0 . 0 8 4 8 4 5 1 0 6 9 0 8 7 km
Hints:
1 import j a v a . u t i l . Sc anne r ;
3 p u b l i c c l a s s DistanceTwoPointsOnSurfaceOfEarth {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
5 S ca nner i n p u t = new S cann er ( System . i n ) ;
System . out . p r i n t ( " I n p u t t h e l a t i t u d e o f c o o r d i n a t e 1 : " ) ;
7 d o u b l e l a t 1 = i n p u t . nextDouble ( ) ;
System . out . p r i n t ( " I n p u t t h e l o n g i t u d e o f c o o r d i n a t e 1 : " ) ;
9 d o u b l e l o n g 1 = i n p u t . nextDouble ( ) ;
System . out . p r i n t ( " I n p u t t h e l a t i t u d e o f c o o r d i n a t e 2 : " ) ;
11 d o u b l e l a t 2 = i n p u t . nextDouble ( ) ;
System . out . p r i n t ( " I n p u t t h e l o n g i t u d e o f c o o r d i n a t e 2 : " ) ;
13 d o u b l e l o n g 2 = i n p u t . nextDouble ( ) ;
input . c l o s e () ;
15
29 d o u b l e e a r t h R a d i u s = 6 3 7 1 . 0 1 ; // K i l o m e t e r s
31 double d i s t a n c e = . . . ;
return distance ;
33 }
}
15
HaQT Fundamental Programming
1.16 CheckEnvironmentVariables
Write a Java program to get the value of the environment variable PATH, TEMP, USERNAME.
Sample output:
Command window
Environment v a r i a b l e PATH:
2 / u s r / l o c a l / s b i n : / u s r / l o c a l / b i n : / u s r / s b i n : / u s r / b i n : / s b i n : / b i n : / u s r / games
: / u s r / l o c a l / games
4 Environment v a r i a b l e TEMP:
null
6 Environment v a r i a b l e USERNAME:
null
Hints:
1 p u b l i c c l a s s CheckEnvironmentVariables {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 // Gets t h e v a l u e o f t h e s p e c i f i e d environment v a r i a b l e "PATH"
System . out . p r i n t l n ( " \ nEnvironment v a r i a b l e PATH: " ) ;
5 System . out . p r i n t l n ( System . g e t e n v ( "PATH" ) ) ;
11 // g e t s t h e v a l u e o f t h e s p e c i f i e d environment v a r i a b l e "USERNAME"
System . out . p r i n t l n ( " \ nEnvironment v a r i a b l e USERNAME: " ) ;
13 System . out . p r i n t l n ( . . . ) ;
}
15 }
1.17 CheckJavaSystem
Write a Java program to check whether Java is installed on your computer.
Sample output:
Command window
1 Java V e r s i o n : 1 . 8 . 0 _71
Java Runtime V e r s i o n : 1 . 8 . 0 _71=b15
3 Java Home : / opt / j d k / j d k 1 . 8 . 0 _71/ j r e
Java Vendor : O r a c l e C o r p o r a t i o n
5 Java Vendor URL: h t t p : / / Java . o r a c l e . com/
Java C l a s s Path : .
Hints:
16
HaQT Fundamental Programming
p u b l i c c l a s s CheckJavaSystem {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
System . out . p r i n t l n ( " \ nJava V e r s i o n : " + System . g e t P r o p e r t y ( " j a v a . v e r s i o n " ) ) ;
4 System . out . p r i n t l n ( " Java Runtime V e r s i o n : " + . . . ) ;
System . out . p r i n t l n ( " Java Home : " + . . . ) ;
6 System . out . p r i n t l n ( " Java Vendor : " + . . . ) ;
System . out . p r i n t l n ( " Java Vendor URL: " + . . . ) ;
8 System . out . p r i n t l n ( " Java C l a s s Path : " + . . . + " \n" ) ;
}
10 }
1.18 CheckAsciiTable
Write a program that accepts a character from the user and print ascii value of the given character.
Sample output:
Command window
Input a c h a r a c t e r : Z
2 The ASCII v a l u e o f Z i s : 9 0
Hints:
17
HaQT Fundamental Programming
Scanner class in Java supports nextInt(), nextLong(), nextDouble() etc. But there is no nextChar().
To read a char, we use next().charAt(0). Function next() returns the next token/word in the input as
a string and charAt(0) function returns the first character in that string.
import j a v a . u t i l . Sc anne r ;
2
public c l a s s CheckAsciiTable {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
S ca nner s c = new Scan ner ( System . i n ) ;
6
// C h a r a c t e r i n p u t
8 c h a r inputChar = s c . n e x t ( ) . charAt ( 0 ) ;
10 int asciiValue = . . . ;
12 // P r i n t t h e r e a d v a l u e
System . out . p r i n t l n ( "The ASCII v a l u e o f " + inputChar + " i s : " + . . . ) ;
14 }
}
18