Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Keshav_Java

The document contains a series of practical Java programming exercises, each demonstrating different concepts such as command line arguments, variable manipulation, input handling, and exception management. Each practical includes a code snippet that illustrates the specific task to be accomplished, such as calculating averages, checking for palindromes, and demonstrating inheritance and interfaces. The exercises range from basic to more advanced topics, providing a comprehensive overview of Java programming techniques.

Uploaded by

keshavkarn173
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Keshav_Java

The document contains a series of practical Java programming exercises, each demonstrating different concepts such as command line arguments, variable manipulation, input handling, and exception management. Each practical includes a code snippet that illustrates the specific task to be accomplished, such as calculating averages, checking for palindromes, and demonstrating inheritance and interfaces. The exercises range from basic to more advanced topics, providing a comprehensive overview of Java programming techniques.

Uploaded by

keshavkarn173
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

PRACTICAL - 01

1. Write a program in java which prints your name using command line arguments.

public class PrintName {

public static void main(String[] args) {

if (args.length > 0) {

System.out.println(args[0]);

}
PRACTICAL - 02
2. Write a program in java which enters three number using command line arguments and print
average of the number

public class Threenumavg {

public static void main(String[] args) {

if (args.length == 3) {

double first = Double.parseDouble(args[0]);

double second = Double.parseDouble(args[1]);

double third = Double.parseDouble(args[2]);

double average = (first + second + third) / 3;

System.out.println(average);

}
PRACTICAL - 03
3. Write a program to swap the value of 2 variables without using 3rd variable

public class Swapping {

public static void main(String[] args) {

int first = 10;

int second = 20;

first = first + second;

second = first - second;

first = first - second;

System.out.println("first = " + first);

System.out.println("second = " + second);

}
PRACTICAL - 04
4. Write a program to calculate the sum of digits of a given integer no

import java.util.Scanner;

public class DigitsSum {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int new_nums = sc.nextInt();

int sum = 0;

while (new_nums != 0) {

sum += new_nums % 10;

new_nums /= 10;

System.out.println(sum);

}
PRACTICAL - 05
5. Write a program to compute the sum of the first and last digit of a given number.

import java.util.Scanner;

public class SumDigits {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int nums = sc.nextInt();

int last = nums % 10;

while (nums >= 10) {

nums /= 10;

int first = nums;

System.out.println(first + last);

}
PRACTICAL - 06
6. Write a program in java which enter the number using Data Input Stream and check entered
number is even or odd.

import java.io.DataInputStream;

public class CheckOddEven {

public static void main(String[] args) throws Exception {

DataInputStream dis = new DataInputStream(System.in);

int number = Integer.parseInt(dis.readLine());

if (number % 2 == 0) {

System.out.println("Even");

} else {

System.out.println("Odd");

}
PRACTICAL - 07
7. Write an application that reads a string and determines whether it is a palindrome.

import java.util.Scanner;

public class CheckingPalindrome {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String str_name = sc.nextLine();

String reversed_str = new StringBuilder(str_name).reverse().toString();

if (str_name.equals(reversed_str)) {

System.out.println("Palindrome");

} else {

System.out.println("Not a Palindrome");

}
PRACTICAL - 08
8. Write a program to enter a sentence form keyboard and also find all the words in that sent
starting character as vowel

import java.util.Scanner;

public class VowelCheck {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String new_word = sc.nextLine();

String[] vowel_words = new_word.split(" ");

for (String isword : vowel_words) {

char firstChar = Character.toLowerCase(isword.charAt(0));

if (firstChar == 'a' || firstChar == 'e' || firstChar == 'i' || firstChar == 'o' || firstChar


== 'u') {

System.out.println(isword);

}
PRACTICAL - 09
9. Write a Program in java which creates the array of size 5; find the sum and average o numbers.

import java.util.Scanner;

public class SumandAvg {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int[] nums = new int[5];

int sum = 0;

for (int i = 0; i < 5; i++) {

nums[i] = sc.nextInt();

sum += nums[i];

double average = sum / 5.0;

System.out.println("Sum: " + sum);

System.out.println("Average: " + average);

}
PRACTICAL - 10
10. Create a java program that has three version of add method which can add two, three,
integers.

public class Adding {

public int add(int first, int second) {

return first + second;

public int add(int first, int second, int third) {

return first + second + third;

public static void main(String[] args) {

Adding obj = new Adding();

System.out.println(obj.add(4, 16));

System.out.println(obj.add(2,7,9));

}
PRACTICAL - 11
11. Program illustrating Classes and Objects.

class Pirate {

String Pirate_name;

int bounty;

void display() {

System.out.println("Pirate: " + Pirate_name);

System.out.println("Bounty: " + bounty);

public static void main(String[] args) {

Pirate New_gen = new Pirate();

New_gen.Pirate_name = "Monkey D. Luffy";

New_gen.bounty = 320;

s1.display();

}
PRACTICAL - 12
12. Program illustrating Method Overloading and Method Overriding.

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat Meows");
}
void sound(String type) {
System.out.println("Cat " + type + "s");
}
public static void main(String[] args) {
Animal a = new Animal();
a.sound();
Cat c = new Cat();
c.sound();
c.sound("meow meow");
}
}
PRACTICAL - 13
13. Program illustrating concept of Interface.

interface Anime {

void Anime_name();

class Newgen_Anime implements Anime {

public void Anime_name() {

System.out.println("Demon Slayer");

public class InterfaceExample {

public static void main(String[] args) {

Anime new_name = new Newgen_Anime();

New_name.Anime_name();

}
PRACTICAL - 14
14. Program illustrating use of Final and Super keyword.

class Parent {

final int a = 26;

void display() {

System.out.println("Parent class");

class Child extends Parent {

void display() {

super.display();

System.out.println("Child class");

public static void main(String[] args) {

Child new_obj = new Child();

New_obj.display();

// a = 35; // Error: You Cannot assign a value to final variable 'a'

}
PRACTICAL - 15
15. Program that illustrates the Creation of simple package.

package mypackage;

public class Sample_Package {

public void anime_name() {

System.out.println("The famous anime name is Dragon Ball!");

}
PRACTICAL - 16
16. Program that illustrates the Accessing of a package.

import mypackage.HelloWorld;

public class AccessPackage {

public static void main(String[] args) {

Sample_Package new_name = new Sample_Package();

new_name.anime_name();

}
PRACTICAL - 17
17. Program that illustrates the Handling of predefined exceptions.

import java.util.Scanner;

public class PreException_example {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

try {

int first = 24;

int second = sc.nextInt();

System.out.println(first / second);

} catch (ArithmeticException e) {

System.out.println("Error: Division by zero");

}
PRACTICAL - 18
18. Program that illustrates the Handling of user defined exceptions.

class InvalidAnimeException extends Exception {

public InvalidAnimeException(String message) {

super(message);

public class UserException_example {

public static void main(String[] args) {

try {

String name = "Haikyuu";

if (!name.equals("Berserk")) {

throw new InvalidAnimeException("Invalid Anime Name");

catch (InvalidAnimeException e) {

System.out.println(e.getMessage());

You might also like