[Go to site: main page, start]

0% found this document useful (0 votes)
5 views18 pages

Java Programming Exercises Overview

Java practical file for colleges

Uploaded by

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

Java Programming Exercises Overview

Java practical file for colleges

Uploaded by

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

VIVEK SHARMA (2309005370095)

1) Write a program that takes your full name as input and displays
the abbreviations only. For example, if your name is Ramesh Chandra
Sharma, then the output should be [Link].

import [Link];​

public class NameAbb{​
public static void main(String[] args) {​
Scanner scanner = new Scanner([Link]);​

[Link]("Enter your full name: ");​
String fullName = [Link]();​

String[] nameParts = [Link]().split("\\s+");​

StringBuilder abb= new StringBuilder();​

for (int i = 0; i < [Link] - 1; i++) {​

[Link]([Link](nameParts[i].charAt(0))).append('.');​
}​

[Link](nameParts[[Link] - 1]);​

[Link]("Abbreviated Name: " + [Link]());​
}​
}

Enter your full name: Ankit Kumar Saxena

Abbreviated Name: [Link]

1​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

2) Write a program to check whether a letter 'a' is present in the


string, given through Command Line Argument also count the
occurrence of ‘a’ in the string.

public class checkLetter {​


public static void main(String[] args) {​
if([Link]==0){​
[Link]("Please provide string: ");​
return;​
}​

String input = args[0];​
int count = 0;​
boolean found = false;​

for(int i = 0; i < [Link](); i++){​
char ch = [Link](i);​
if(ch == 'a' || ch == 'A'){​
count++;​
found = true;​
}​
}​

if(found){​
[Link]("letter 'a' is present in the string.");​
[Link]("Occurrence of letter 'a' in string is" +
" " + count);​
}else{​
[Link]("letter 'a' is not present in the
string.");​
}​
}​
}

PS C:\Users\LENOVO\Desktop\JavaCodes> javac [Link] ​


PS C:\Users\LENOVO\Desktop\JavaCodes> java checkLetter "I love java
programming language"​
letter 'a' is present in the string.​
Occurrence of letter 'a' in string is 5

2​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

3) Write a program to delete all vowels from the string "Hello, have a
good day"

public class remVowels {​


public static void main(String[] args) {​
String input = "Hello, have a good day";​
StringBuilder res = new StringBuilder();​

for(int i = 0; i < [Link](); i++){​
char ch = [Link](i);​

if(!isVowel(ch)){​
[Link](ch);​
}​
}​
[Link]("Result after removing vowels: " +
[Link]());​
}​
public static boolean isVowel(char ch) {​
ch = [Link](ch);​
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch ==
'u';​
}​
}

PS C:\Users\LENOVO\Desktop\JavaCodes> javac [Link]​


PS C:\Users\LENOVO\Desktop\JavaCodes> java remVowels​
Result after removing vowels: Hll, hv gd dy

3​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

4) Create a class Complex with two data members “real” and “img”.
incr() -- increase value of real and img by 1.
display() - display complex numbers like : real=
4, imaginary= 6
setData(int re, int im) - initiate the value of real
by re and img by im.
Add(Complex) - add two objects and return the
result.
Write all possible constructors.

public class Complex {​


private int real;​
private int img;​

// Default Constructor​
public Complex(){​
[Link] = 0;​
[Link] = 0;​
}​

// Parameterized Constructor​
public Complex(int real, int img){​
[Link] = real;​
[Link] = img;​
}​

// Copy Constructor​
public Complex(Complex other){​
[Link] = [Link];​
[Link] = [Link];​
}​

// set data method​
public void setData(int re, int im){​
[Link] = re;​
[Link] = im;​
}​
// increment method for real and image part ​
public void incr(){​
real++;​
img++;​
}​

4​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)


// display method​
public void display(){​
[Link]("Real: " + real +", Image: " + img);​
}​

// add complex method​
public Complex Add(Complex c) {​
return new Complex([Link] + [Link], [Link] + [Link]);​
}​
//Main method​
public static void main(String[] args) {​
Complex c1 = new Complex();​
[Link](2, 3);​
[Link]();​

Complex c2 = new Complex(4, 5);​
[Link]();​

Complex c3 = [Link](c2);​
[Link]();​

[Link]();​
[Link]();​

Complex c4 = new Complex(c3);​
[Link]();​
}​
}

PS C:\Users\LENOVO\Desktop\JavaCodes> javac [Link]​


PS C:\Users\LENOVO\Desktop\JavaCodes> java Complex ​
Real: 2, Image: 3​
Real: 4, Image: 5​
Real: 6, Image: 8​
Real: 7, Image: 9​
Real: 7, Image: 9

5​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

5) Extend the above class by creating a new class


“AdvanceComplex”. Write a method “multiply” to multiply two
complex values.
// Derived Class from Complex Class defined in last program​
public class AdvanceComplex extends Complex{​

public AdvanceComplex(){​
super(); //Default Constructor​
}​

public AdvanceComplex(int real, int img){​
super(real, img); //Parameterized Constructor​
}​

public AdvanceComplex(AdvanceComplex other){​
super(other); //Copy Constructor​
}​

public AdvanceComplex multiply(AdvanceComplex c){​
int newReal = ([Link] * [Link]) - ([Link] * [Link]);​
int newImg = ([Link] * [Link]) + ([Link] * [Link]);​
return new AdvanceComplex(newReal, newImg);​
}​

@Override​
public void display() {​
[Link](real + " + " + img + "i");​
}​

public static void main(String[] args) {​
AdvanceComplex a1 = new AdvanceComplex(3, 2);​
AdvanceComplex a2 = new AdvanceComplex(1, 7);​

[Link]();​
[Link]();​

AdvanceComplex result = [Link](a2);​
[Link]("Multiplication Result: ");​
[Link]();​
}​
}

6​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

PS C:\Users\LENOVO\Desktop\JavaCodes> javac [Link]


[Link]​
PS C:\Users\LENOVO\Desktop\JavaCodes> java AdvanceComplex ​
3 + 2i​
1 + 7i​
Multiplication Result: -11 + 23i

7​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

6) Write an interface : “Shape”.


interface Shape {
public final double pi=22.0/7;
public void perimeter();
public double area();
}
Now implement the above interface and create a Circle class. And find out
the area and perimeter of an object of radius 7.
// Shape Interface [Link]​
public interface Shape {​
public double pi = 22.0/7;​

public void perimetre();​
public double area();​
}

// Circle Class implementing the Shape Interface [Link]​


public class Circle implements Shape{​
private double radius;​

public Circle(double radius){​
[Link] = radius;​
}​

public void perimetre(){​
double peri = 2 * pi *radius;​
[Link]("Perimetre is: "+ peri);​
}​

public double area(){​
double ar = pi * radius * radius;​
[Link]("Area is: "+ ar);​
return ar;​
}​

public static void main(String[] args) {​
Circle c = new Circle(7);​
[Link]();​
[Link]();​
}​
}

8​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

PS C:\Users\LENOVO\Desktop\JavaCodes> javac [Link] [Link]​


PS C:\Users\LENOVO\Desktop\JavaCodes> java Circle​
Perimetre is: 44.0​
Area is: 154.0

9​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

7) Create a package pkg and Create two Alpha and Beta classes. Write a
display function in both the classes which print the class name. Now create
a new class Gamma, outside the package and execute display methods of
Alpha class as well as Beta class.
// Pkg [Link]​
package pkg;​

public class Alpha {​
public void display(){​
[Link]("This is Alpha Class");​
}​
}

//pkg [Link]​
package pkg;​

public class Beta {​
public void display(){​
[Link]("This is Beta Class");​
}​
}

//[Link]​
import [Link];​
import [Link];​
public class Gamma {​
public static void main(String[] args) {​
Alpha a = new Alpha();​
Beta b = new Beta();​
[Link]();​
[Link]();​
}​
}

PS C:\Users\LENOVO\Desktop\JavaCodes> javac .\pkg\*.java .\[Link];


java Gamma​
This is Alpha Class​
This is Beta Class

10 ​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

8) Write a main function to divide two numbers. Handle the exception if


denominator is zero using Exception handling.

import [Link];​

public class Division {​
public static void main(String[] args) {​
Scanner sc = new Scanner([Link]);​

try{​
[Link]("Enter Numerator: ");​
int num = [Link]();​

[Link]("Enter Denominator: ");​
int den = [Link]();​

int result = num / den;​
[Link]("Result is: "+ result);​
}catch(ArithmeticException e){​
[Link]("Error: Cannot Divide by 0...");​
}finally{​
[Link]();​
}​
}​
}

PS C:\Users\LENOVO\Desktop\JavaCodes> javac [Link]​


PS C:\Users\LENOVO\Desktop\JavaCodes> java Division ​
Enter Numerator: ​
12​
Enter Denominator: ​
0​
Error: Cannot Divide by 0...

11 ​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

9) Prepare an awt frame with a button on it. Apply listener on the button.
import [Link];​
import [Link];​
import [Link];​
import [Link].*;​

public class awtProgram extends Frame implements ActionListener{​
Button btn;​
Label lbl;​

public awtProgram(){​
setTitle("My Frame");​
setSize(300, 200);​
setLayout(null);​
setVisible(true);​

addWindowListener(new WindowAdapter() {​
public void windowClosing(WindowEvent e){​
dispose();​
}​
});​

btn = new Button("Button");​
[Link](100, 80, 100, 30);​
add(btn);​

lbl =new Label();​
[Link](100, 120, 200, 30);​
add(lbl);​

[Link](this);​
}​
public void actionPerformed(ActionEvent e){​
[Link]("Button Clicked!");​
}​

public static void main(String args[]){​
new awtProgram();​
}​
}

12 ​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

PS C:\Users\LENOVO\Desktop\JavaCodes> javac [Link]​


PS C:\Users\LENOVO\Desktop\JavaCodes> java awtProgram

13 ​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

10) Create a frame. Take TextField, Label. When you write on TextField
characters also appear on Label also. If we write “Exit” in TextField
program terminates.

import [Link];​
import [Link];​
import [Link];​
import [Link];​
import [Link];​
import [Link];​
import [Link];​

public class awtProgram1 extends Frame implements TextListener{​
TextField tf;​
Label lbl;​

public awtProgram1(){​
setTitle("My Frame");​
setSize(400, 200);​
setLayout(null);​
setVisible(true);​

//Label​
lbl = new Label("Type Here.....");​
[Link](50, 60, 300, 30);​
add(lbl);​

//TextField​
tf = new TextField();​
[Link](50, 100, 300, 30);​
[Link](this);​
add(tf);​

addWindowListener(new WindowAdapter() {​
public void windowClosing(WindowEvent e){​
dispose();​
}​
});​
}​
public void textValueChanged(TextEvent e) {​
String input = [Link]();​
[Link](input);​

14 ​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

if([Link]("Exit")){​
[Link](0);​
}​
}​

public static void main(String[] args) {​
new awtProgram1();​
}​
}

15 ​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

11) Prepare an applet with a button on it. Apply listener on the button.

import [Link];​
import [Link];​
import [Link];​
import [Link];​
import [Link];​

public class javaApplet extends Applet implements ActionListener{​
Button btn;​
String msg = "";​

public void init(){​
btn = new Button("Button");​
add(btn);​
[Link](this);​
}​

public void actionPerformed(ActionEvent e){​
msg = "Button was clicked!";​
repaint();​
}​

public void paint(Graphics g){​
[Link](msg, 100, 100);​
}​
}

16 ​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

12) Write a class Employee. Take String name and int age as two members
of this class. If the age is above 60, an exception occurs. (“Hint : create
your own exception class”).
// age/[Link]​
package age;​

public class ageLimit extends Exception{​
public ageLimit(String message){​
super(message);​
}​
}

// [Link]​
import [Link];​

public class Employee {​
private String name;​
private int age;​

public Employee(String name, int age) throws ageLimit{​
if(age > 60){​
throw new ageLimit("Age cannot be more than 60");​
}​
[Link] = name;​
[Link] = age;​
}​
public void display() {​
[Link]("Name: " + name + ", Age: " + age);​
}​

// Main method​
public static void main(String[] args) {​
try{​
Employee emp1 = new Employee("Vivek", 20);​
[Link]();​

Employee emp2 = new Employee("Rahul", 61);​
[Link]();​
}catch(ageLimit e){​
[Link]("Exception: "+ [Link]());​
}​
}​
}

17 ​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA


VIVEK SHARMA (2309005370095)

PS C:\Users\LENOVO\Desktop\JavaCodes> javac ./age/*.java [Link]​


PS C:\Users\LENOVO\Desktop\JavaCodes> java Employee ​
Name: Vivek, Age: 20​
Exception: Age cannot be more than 60

18 ​ ​ ​ ​ ​ OBJECT ORIENTED PROGRAMMING WITH JAVA

You might also like