Core JAVA

Summary

Duration: 30 hrs

Fee : Contact

Updated course content click here

Note:

1-15 topics will be covered in this course

16-20 are the advanced topics which would be covered on student’s interest.

Core Java Exercises

Data Type, Variables

Exercise 1: Which data type would you use to represent the following values?

a) Child age

b) Employee salary

c) Whether somebody has children or not

d) Apartment letter

Exercise 2: Declare two float variables (x and y) and assign them values 2.5 and 5.4 (with separated declaration and initialization)

Exercise 3: Initialize a character variable with some value say "X" and increment its value.

Exercise 4: Calculate the hypotenuse of a right angle triangle when its perpendicular and base are given(in double data type).

Operators

Exercise 1: Write Java program to allow the user to input two integer values and then the program prints the results of adding, subtracting, multiplying, and dividing among the two values.

Exercise 2: Write Java program to generate a random number between 1 to 6.

Exercise 3: Write Java program to allow the user to input two float values and then the program adds the two values together. The result will be assigned to the first variable.Example output is as follows

Enter value a:12.5

The value of a before adding is 12.5.

Enter value b:34.9

The value of a after adding is 47.4.

Exercise 4: Write Java program to allow the user to input the amount of deposit, yearly interest rate (percentage), and income tax(percentage). Then the program will calculate the amount of interest that the person earns in the year. See the example output below:

The amount of deposit: 1000

Yearly interest rate: 7.5%

Income tax rate: 4%

The amount of interest earned in the year:71.0

Conditional Statements

Exercise 1: Write Java program to allow the user to input his/her age. Then the program will show if the person is eligible to vote. A person who is eligible to vote must be older than or equal to 18 years old.

Output:

Enter your age: 18

You are eligible to vote.

Exercise 2: Write a Java program to determine whether an input number is an even number.

Exercise 3: Write a Java program that determines a student’s grade.

The program will read three types of scores(quiz, mid-term, and final scores) and determine the grade based on the following rules:

-if the average score >=90% =>grade=A

-if the average score >= 70% and <90% => grade=B

-if the average score>=50% and <70% =>grade=C

-if the average score<50% =>grade=F

Switch Statement

Exercise 1: Write a Java program that allows the user to choose the correct answer of a question.

See the example below:

What is the correct way to declare a variable to store an integer value in Java?

a. int 1x=10;

b. int x=10;

c. float x=10.0f;

d. string x="10";

Enter your choice: c

For loop

Exercise 1: Write a Java program to find sum of ten numbers starting from 1 using for loop.

Exercise 2: Write a Java program to find the average of ten numbers starting from 1 using for loop.

While loop

Exercise 1: Write Java program to prompt the user to choose the correct answer from a list of answer choices of a question.

The user can choose to continue answering the question or stop answering it. See the example below:

What is the command keyword to exit a loop in Java?

a. int

b. continue

c. break

d. exit

Enter your choice: b

Incorrect!

Again? press y to continue:

Arrays

Exercise 1: Initialise an array with the marks of 5 subjects and calculate the average.

Exercise 2: Find the sum of n numbers using Arrays.

Output:

Enter number of values : 5

Enter 5 numbers :

10

20

30

25

40

Sum of 5 number is : 125

Exercise 3: Find the largest of n numbers using Arrays.

Output:

Enter number of values : 5

Enter 5 numbers :

10

20

30

25

40

Largest number is: 40

Exercise 3: By using the sequential search algorithm, write a Java program to search for an element of an integer array of 10 elements.

Exercise 4: Modify the Java code in exercise 3 to search for an element of the array by using binary search algorithm.

Objects and Classes

Exercise 1: Write a program to create a room class, the attributes of this class is roomno, roomtype, roomarea and ACmachine. In this class the member functions are setdata and displaydata.

Exercise 2: Write a program create a class ‘simpleobject‘. Using constructor display the message.

Exercise 3: Write a program to give the example for ‘this’ operator. And also use ‘this’ keyword as return statement.

Exercise 4: Create class point with following instance variable and methods.

Instance variable: private int x,y

Constructors : public Point(), Point(int x, int y)

Methods : public void setX(int x), setY(int y), setXY(int x,

int y)

Inheritance, Polymorphism

Exercise 1: Create an Animal class with constructor and public methods like sleep and eat. Add only System out statements to say method is called.

Create Dog class which inherits Animal properties. Create main class that creates an instance of Dog class and invoke sleep and eat methods.

Exercise 2: Create an Animal class with constructor and public methods like sleep and eat. Add only System out statements to say method is called.

Create Dog class which extends Animal class and override sleep and eat methods with System out statements. Create main class that creates an instance of Dog class and invoke sleep and eat methods.

Exercise 3: Create a class Circle with three integer variables like radius, x, y and two constructors, one without parameters and one with a parameter called radius. Create main class then create two instances of Circle with and without parameter

Exercise 4: Modify Exercise 1 to create one private method sleep and eat method as public.

Strings

Exercise 1: Write a program to find the length of string.

Exercise 2: Write a program to concatenate two strings.

Exercise 3: Write a program to extract last four letters from a String “Welcome”.

Exercise 4: Write a program to find number of vowels in “Computer” word.

Exercise 3: Write a program to do the following String operation.

Java String Compare,

Java String Concat,

Java String Contains,

String Length,

String Replace,

Java String Reverse

Packages and Interfaces

Exercise 1: Create a package named com.test and create a Test class with two members and display method. Create another package com.test1 and create a class Test1, in this class import Test class and create an instance of Test class and call display method.

Exercise 2: Define an interface having one method that takes an integer parameter. For this method, provide two implementations: In the first one,just print the value and in the second one, print the square of the number. Try to call both the versions.

Exercise 3: Create an interface with name "Shape" and declare a public method findArea.

Create a class with name "Circle" and create one private integer member as radius and assign some value to it. Implement Shape interface in Circle class and implement findArea mehtod with radius value.

Create a class with name "Rectangle" and create two private integer members as width and height and assign some values to them. Implement Shape interface in Circle class and implement findArea mehtod with rawidth and height values.

Create a Test class with main method where use Sahpe interface to create Circle and Rectangle objects and find areas using findArea method.

Access Modifiers

Exercise 1: Write a program that includes all the access modifiers.

Exception Handling

Exercise 1: Define an object reference and initialize it to null. Try to call a method through this reference. Now wrap the code in a try-catch clause to catch the exception.

Exercise 2: Create your own exception class using the extends keyword. Write a constructor for this class that takes a String argument and stores it inside the object with a String reference. Write a method that prints out the stored String. Create a try-catch clause to exercise your new exception.

Exercise 3: Write a class with a method that throws an exception of the type created in Exercise 2. Try compiling it without an exception specification to see what the compiler says. Add the appropriate exception specification. Try out your class and its exception inside a try-catch clause.

Exercise 4: Write a Java program that shows that the order of exception handlers is important. If you try to catch a superclass exception type before a subclass type, the compiler should generate errors.

Exercise 5: Write a Java program that demonstrate both checked exceptions and runtime exception.

Threads

Exercise 1: Create a simple class "Fruit" that extends Thread and print name of a fruit at timed intervals. Fruit class contains two member variables fruitName, pauseSeconds. Create constructor to set these two values. Override run method in which create a loop to print fruitname at every pauseSeconds.

Create Test call with main method, create three Fruit objects with different values of fruitName, pauseSeconds and start three threads.

Exercise 2: To illustrate creation of multiple threads in a program performing concurrent operations, let us consider the processing of the following mathematical equation:

p = sin (x) + cos (y) + tan (z)

As these trigonometric functions are independent operations without any dependencies between them, they can be executed concurrently. After that their results can be combined to produce the final result.

Write individual threads for sin, cos and tan functions. We can use "Math" class to find sin, cos and tan values of x,y,z. Create a Test class that creates sin, cos, tan threads and start them. Wait for all threads to complete their job using join method and final calculate p value

Exercise 3: Write a program for interthread communication process. In this theyhave three classes consumer, producer and stock.

Producer -> addStock() -> Stock -> getStock() -> Consumer

---------- ----------

Notify() wait()

Applets

Exercise 1: Write a Applet program that automatically display the text with FontStyle, Font type .

Exercise 2: Write a program to create a Applet life cycle.

Exercise 3: Write a Applet program that automatically display the text with Font Style, Font type Using getParameter Method.

Exercise 4: Write a program that has menubar and also a quit option and if the clicks the quite option the applet should quit.

File IO and Streams

Exercise 1: Write a program to create a text file in the path c:\java\abc.txt and check whether that file is exists. Using the command exists(), isDirectory(), isFile(), getName() and getAbsolutePath().

Exercise 2: Write a program to create a directory and check whether the directory is created.

Exercise 3: Write a program to create a file and write data into it using the methods OutputStream class

Exercise 4: Write an example that counts the number of times a particular character, such as e, appears in a file. The character can be specified at the command line.

Serialization

Exercise 1: Create a class Employee with two member variables empId and empName and create a constructer to set these values. Implement Serializable and store Employee objects into a file.