0% found this document useful (0 votes)
12 views19 pages

Java Programming Exercises Overview

The document outlines a series of programming exercises focused on Java concepts, including conditional statements, loops, strings, classes, constructors, command line arguments, method overloading, inheritance, interfaces, I/O streams, collections, exception handling, multithreading, applets, and AWT controls. Each exercise includes objectives and key competencies that students should achieve, along with example code snippets and expected outputs. The exercises are designed to enhance understanding and practical skills in Java programming.

Uploaded by

Cherry Jagath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views19 pages

Java Programming Exercises Overview

The document outlines a series of programming exercises focused on Java concepts, including conditional statements, loops, strings, classes, constructors, command line arguments, method overloading, inheritance, interfaces, I/O streams, collections, exception handling, multithreading, applets, and AWT controls. Each exercise includes objectives and key competencies that students should achieve, along with example code snippets and expected outputs. The exercises are designed to enhance understanding and practical skills in Java programming.

Uploaded by

Cherry Jagath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Exp.

Name of the Objectives Key Competencies


No. experiment

1 Exercise programs on (a)Write program using if (a) Know the usage of IF and switch
conditional statements statement and switch statements.
and loop statements. (b)Write program using while, ( b)Compile the program and rectify the
do and for constructs. errors.
(c) Observe the output.
2 Exercise programs on (a)Write a programs to (a) Create String objects
Strings. manipulate Strings ( b)Use string class
(b)Write a programs to methods Observe the
arrange array of strings in output.
ascending order
3 Exercise program to (a)Write a program to create a (a) Create class.
create class and objects class and create objects. (b) Declare methods.
and adding methods. (b)Write a program to create (c) Create objects.
class adding methods and (d) Write main method.
access class members. (e) Access class members.
4 Exercise programs (a)Write a program using (a) Declare and define constructor.
using constructors and default constructor. (b) Call default constructor.
construction over (b)Write a program using (c) Call parameterized constructor.
loading. parameterized constructor. (d) observe constructor overloading.

5 Exercise programs on (a)Write a program to (a) Use command line arguments.


command line illustrate usage of (b) Run the program.
arguments. command line (c) Understand usage of Files.
arguments. (c) Observe the output.
(b)Write a program to read
data as command line
arguments and update
it into Files.
6 Exercise programs (a)Write a program to (a) Observe method overloading.
using concept of illustrate method (b) Overload constructor methods.
overloading methods. overloading.
( b)Write a program
to illustrate
method
overloading using
constructors.
7 Exercise on inheritance. (a)Write a program to (a) Create base class.
illustrate single inheritance. (b) Write base class constructor.
(b)Write a program to (c) Create derived class.
illustrate multiple (d) Use extends keyword.
inheritance. (e) Use super keyword.
(f) Write derived class constructor.
8 Write a program using Write a program using the (a) Use method overriding.
the concept of method concept of method (b) Use this keyword.
overriding. overriding. (c) use super keyword
9 Exercise on importing Write a program to create and (a) Create package.
packages. importing package. (b) Use of access specifiers.
(b) Use package.
(c) Use import keyword.

10 Exercise on interfaces. Write a program to illustrate (a) Define interface.


multiple inheritance using (b) Use extends keyword.
interfaces. (c) Use implements keyword.
(d) Access interface variables.
11 Exercise programs on (a)Write a program to give (a) Use different data types.
I/O Streams values to variables (b) Use read Line() method.
interactively through the (c) Use print ln() method. ( d)use File
keyboard. Streams
(b)Write program to read and Observe the output.
write primitive data types.
(c) Write programs to handle
Files.
12 Exercise programs on (a)Write a java program to (a) Define collection classes
Collections. search a student mark (b) use Array List, Linked List
percentage based on pin (c) apply List and Iterator Interface
number using Array list.
( b)Write a java program to
create linked list to perform
delete, insert, and update
data in linked list with any
application.
13 Exercise on exception (a)Write a program to (a) Use try – catch.
handling illustrate exception (b) Use multiple catch blocks.
handling. (c) Use finally statement. (d) use
(b)Write a program to Nested try
illustrate exception
handling using multiple
catch statements.
(c) Write a program to
illustrate exception
handling using nested try.
14 Exercise on (a)Write a program to create (a) Use extends, new.
multithreading single a thread by (b) Use run() and start() methods.
extending the thread class. (c) Observe thread execution.
(b)Write a program to create a (d) Use implements runnable interface.
single thread by (e) Use set Priority() and get Priority()
implementing the runnable methods.
interface. (f) use wait(),notify() methods
(c) Write a program to create
multiple threads.
( d)Write a program to
illustrate thread priorities.
15 Exercise on applets. Write a program to create (a) Use <applet>…</applet> tag.
simple applet to display (b) Add applet to html file.
different shapes with (c) Run the applet.
colors. (d) use graphics methods
Write an applet program to (e) use threads and graphics.
design simple animation.
16 Exercise on AWT (a)Write an applet program to (a) Use keyboard event methods
controls handle key events. ( b)Use mouse event methods
(b)Write an applet program to (c) Use Text Field class methods
handle mouse events. ( d)Use button class methods
(c) Write an applet program to ( e)Use Check box and List class methods
illustrate Text Field and
button control.
( d)Write an applet program
to illustrate Check box and
List control.
(e )Write an applet program to
illustrate multiple controls.

Write program using if statement


public class IfExample {
public static void main(String[] args) {
//defining an 'age' variable
int age=20;
//checking the age
if(age>18){
[Link]("Age is greater than 18");
}
}
}
Output:
Age is greater than 18

Write program using switch statement

public class SwitchExample {


public static void main(String[] args) {
//Declaring a variable for switch expression
int number=20;
//Switch expression
switch(number){
//Case statements
case 10: [Link]("10");
break;
case 20: [Link]("20");
break;
case 30: [Link]("30");
break;
//Default case statement
default:[Link]("Not in 10, 20 or 30");
}
}
}
Output:
20

Write program using while, do and for constructs


While
class Main {
public static void main(String[] args) {

// declare variables
int i = 1, n = 5;

// while loop from 1 to 5


while(i <= n) {
[Link](i);
i++;
}
}
}
Output
1
2
3
4
5
Do-while
mport [Link];

// Program to find the sum of natural numbers from 1 to 100.

class Main {
public static void main(String[] args) {

int i = 1, n = 5;

// do...while loop from 1 to 5


do {
[Link](i);
i++;
} while(i <= n);
}
}
Output
1
2
3
4
5
Nested for loop in Java

public class Main {


public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
[Link](j + " ");
}
[Link]();
}
}
}
Output
1
12
123
1234
12345
public class StrCompare {
public static void main(String[] args) {
String string1 = "Hello";
String string2 = "hello";
String string3 = [Link]();
String string4 = [Link]();
int i = [Link](string4);//0 for both are equal
if(i==0){
[Link]("Both strings are equal.");
}else{
[Link]("Strings are not equal.");
}
}
}
Output:
Both strings are equals
public class StrConcat {
public static void main(String[] args) {
String str1= "Hello";
String str2 = "World";
String result = str1+" "+str2;
[Link](result);
}
}
Output:
Hello World
public class StrLenght {
public static void main(String[] args) {
String str1= "Hello World!";
[Link]("The length of the string: "+ [Link]());
[Link]("Actual String: "+str1);
[Link]("String reverse: ");
for(int i = [Link]()-1; i>=0; i--){
[Link]([Link](i));
}
}
}

Output:
The Length of string: 12
Actual string: Hello World!
String reverse:!dlroW olleH
Write a programs to arrange array of strings in ascending order
import [Link];

public class StringArrayInOrder {


public static void main(String args[]) {
String[] myArray = {"JavaFX", "HBase", "OpenCV", "Java", "Hadoop", "Neo4j"};
int size = [Link];

for(int i = 0; i<size-1; i++) {


for (int j = i+1; j<[Link]; j++) {
if(myArray[i].compareTo(myArray[j])>0) {
String temp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = temp;
}
}
}
[Link]([Link](myArray));
}
}

Output
[HBase, Hadoop, Java, JavaFX, Neo4j, OpenCV]
Write a program to create a class and create objects.
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj = new Main();
[Link](myObj.x);
}
}
Output:
5
Write a program to create class adding methods and access class members.
public class Main {
static void myMethod() {
[Link]("Hello World!");
}

public static void main(String[] args) {


myMethod();
}
}

Output:
Hello World!
Write a program using default constructor.
class Bike1{
//creating a default constructor
Bike1(){[Link]("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
Output:
Bike is created
Write a program using parameterized constructor.
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
}
//method to display the values
void display(){[Link](id+" "+name);}

public static void main(String args[]){


//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
[Link]();
[Link]();
}
}
Output:
111 Karan
222 Aryan

Write a program to illustrate usage of command line arguments.


class GFG {

// Main driver method


public static void main(String[] args)
{
// Printing the first argument
[Link](args[0]);
}
}
Output
Java GFG welcome to java
Write a program to illustrate method overloading.
class Adder {
// Method to add two integers
static int add(int a, int b) {
return a + b;
}
// Method to add three integers
static int add(int a, int b, int c) {
return a + b + c;
}
}
public class TestOverloading1 {
public static void main(String[] args) {
// Calling the add method with two integers
[Link]([Link](11, 11)); // Output: 22
// Calling the add method with three integers
[Link]([Link](11, 11, 11)); // Output: 33
}
}
Output:
22
33
Write a program to illustrate single inheritance.
class Animal {
// field and method of the parent class
String name;
public void eat() {
[Link]("I can eat");
}
}

// inherit from Animal


class Dog extends Animal {

// new method in subclass


public void display() {
[Link]("My name is " + name);
}
}

class Main {
public static void main(String[] args) {

// create an object of the subclass


Dog labrador = new Dog();

// access field of superclass


[Link] = "Rohu";
[Link]();

// call method of superclass


// using object of subclass
[Link]();

}
}
Run Code
Output
My name is Rohu
I can eat
Write a program to illustrate multiple inheritance.
Write a program using the concept of method overriding.
class Vehicle{
//defining a method
void run(){[Link]("Vehicle is running");}
}
//Creating a child class
class Bike2 extends Vehicle{
//defining the same method as in the parent class
void run(){[Link]("Bike is running safely");}
public static void main(String args[]){
Bike2 obj = new Bike2();//creating object
[Link]();//calling method
}
}
Output:
Bike is running safely
Write a program to create and importing package.
[Link]
package mypack;
class MyPackageClass {
public static void main(String[] args) {
[Link]("This is my package!");
}
}
Output:
This is my package!
Write a program to illustrate multiple inheritance using interfaces.

interface Walkable {
void walk();
}

interface Swimmable {
void swim();
}

// Implement the interfaces in a class


class Duck implements Walkable, Swimmable {
public void walk()
{
[Link]("Duck is walking.");
}

public void swim()


{
[Link]("Duck is swimming.");
}
}

// Use the class to call the methods from the interfaces


class Main {
public static void main(String[] args)
{
Duck duck = new Duck();
[Link]();
[Link]();
}
}

Output:
Duck is walking.
Duck is swimming.

Write programs to handle Files.

// Importing File Class


import [Link];

class GFG {
public static void main(String[] args)
{

// File name specified


File obj = new File("[Link]");
[Link]("File Created!");
}
}

Output
File Created!
Write a java program to search a student mark percentage based on pin number using Array list.
import [Link];
public class Student_Marks
{
public static void main(String[] args)
{
int n, total = 0, percentage;
Scanner s = new Scanner([Link]);
[Link]("Enter no. of subject:");
n = [Link]();
int marks[] = new int[n];
[Link]("Enter marks out of 100:");
for(int i = 0; i < n; i++)
{
marks[i] = [Link]();
total = total + marks[i];
}
percentage = total / n;
[Link]("Sum:"+total);
[Link]("Percentage:"+percentage);
}
}
Output:

Enter no. of subject:5


Enter marks out of 100:
86
89
91
82
78
Sum:426
Percentage:85
Write a java program to create linked list to perform delete, insert, and update data in linked list
with any application.
public class SinglyLinkedList {
//Represent a node of the singly linked list
class Node{
int data;
Node next;

public Node(int data) {


[Link] = data;
[Link] = null;
}
}

//Represent the head and tail of the singly linked list


public Node head = null;
public Node tail = null;

//addNode() will add a new node to the list


public void addNode(int data) {
//Create a new node
Node newNode = new Node(data);

//Checks if the list is empty


if(head == null) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
[Link] = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}

//display() will display all the nodes present in the list


public void display() {
//Node current will point to head
Node current = head;

if(head == null) {
[Link]("List is empty");
return;
}
[Link]("Nodes of singly linked list: ");
while(current != null) {
//Prints each node by incrementing pointer
[Link]([Link] + " ");
current = [Link];
}
[Link]();
}

public static void main(String[] args) {

SinglyLinkedList sList = new SinglyLinkedList();

//Add nodes to the list


[Link](1);
[Link](2);
[Link](3);
[Link](4);

//Displays the nodes present in the list


[Link]();
}
}
Output:
Nodes of singly linked list:
1234
Write a program to illustrate exception handling.
1. public class JavaExceptionExample{
2. public static void main(String args[]){
3. try{
4. //code that may raise exception
5. int data=100/0;
6. }catch(ArithmeticException e){[Link](e);}
7. //rest code of the program
8. [Link]("rest of the code...");
9. }
10. }
Test it Now
Output:
Exception in thread main [Link]:/ by zero
rest of the code...
Write a program to illustrate exception handling using multiple catch statements.
public class MultipleCatchBlock1 {

public static void main(String[] args) {

try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
[Link]("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
[Link]("Parent Exception occurs");
}
[Link]("rest of the code");
}
}
Output:
Arithmetic Exception occurs
rest of the code
Write a program to create single a thread by extending the thread class.
public class Main extends Thread {
public static void main(String[] args) {
Main thread = new Main();
[Link]();
[Link]("This code is outside of the thread");
}
public void run() {
[Link]("This code is running in a thread");
}
}
Output:
This code is outside of the thread
This code is running in a thread
Write a program to create simple applet to display different shapes with colors.
import [Link].*;
import [Link].*;
public class Shapes extends Applet
{
//Function to initialize the applet
public void init()
{
setBackground([Link]);
}
//Function to draw and fill shapes
public void paint(Graphics g)
{
//Draw a square
[Link]([Link]);
[Link]("Square",75,200);
int x[]={50,150,150,50};
int y[]={50,50,150,150};
[Link](x,y,4);
[Link]([Link]);
[Link](x,y,4);
//Draw a pentagon
[Link]([Link]);
[Link]("Pentagon",225,200);
x=new int[]{200,250,300,300,250,200};
y=new int[]{100,50,100,150,150,100};
[Link](x,y,6);
[Link]([Link]);
[Link](x,y,6);
//Draw a circle
[Link]([Link]);
[Link]("Circle",400,200);
[Link](350,50,125,125);
[Link]([Link]);
[Link](350,50,125,125);
//Draw an oval
[Link]([Link]);
[Link]("Oval",100,380);
[Link](50,250,150,100);
[Link]([Link]);
[Link](50,250,150,100);
//Draw a rectangle
[Link]([Link]);
[Link]("Rectangle",300,380);
x=new int[]{250,450,450,250};
y=new int[]{250,250,350,350};
[Link](x,y,4);
[Link]([Link]);
[Link](x,y,4);
//Draw a triangle
[Link]([Link]);
[Link]("Traingle",100,525);
x=new int[]{50,50,200};
y=new int[]{500,400,500};
[Link](x,y,3);
[Link]([Link]);
[Link](x,y,3);
}
}
/*
<applet code = [Link] width=600 height=600>
</applet>
*/
Write an applet program to handle key events.
import [Link].*;
import [Link].*;
import [Link].*;
public class EventApplet extends Applet implements ActionListener{
Button b;
TextField tf;

public void init(){


tf=new TextField();
[Link](30,40,150,20);
b=new Button("Click");
[Link](80,150,60,50);

add(b);add(tf);
[Link](this);

setLayout(null);
}

public void actionPerformed(ActionEvent e){


[Link]("Welcome");
}
}
[Link]
<html>
<body>
<applet code="[Link]" width="300" height="300">
</applet>
</body>
</html>
Write an applet program to illustrate Check box and List control.
// importing AWT class
import [Link].*;
public class CheckboxExample1
{
// constructor to initialize
CheckboxExample1() {
// creating the frame with the title
Frame f = new Frame("Checkbox Example");
// creating the checkboxes
Checkbox checkbox1 = new Checkbox("C++");
[Link](100, 100, 50, 50);
Checkbox checkbox2 = new Checkbox("Java", true);
// setting location of checkbox in frame
[Link](100, 150, 50, 50);
// adding checkboxes to frame
[Link](checkbox1);
[Link](checkbox2);

// setting size, layout and visibility of frame


[Link](400,400);
[Link](null);
[Link](true);
}
// main method
public static void main (String args[])
{
new CheckboxExample1();
}
}
Output:

You might also like