0% found this document useful (0 votes)
6 views

JAVA EXP 6-9

Uploaded by

Piyush singh
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)
6 views

JAVA EXP 6-9

Uploaded by

Piyush singh
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
You are on page 1/ 5

Program- 6

Aim- Demonstrating Method constructor and overloading.


Programs:
class Method {
void display() {
System.out.println("NO Parameters.");
}
void display(int i) {
System.out.println("one Parameter: " + i);
}
void display(int i, int j) {
System.out.println("Two Parameters: " + i + " and " + j);
}
}
class Constructor {
int x, y;
Constructor() {
x = 5;
y = 7;
System.out.println("Default constructor: x = " + x + " and y = " + y);
}
Constructor(int i) {
x = i;
y = 7;
System.out.println("One Edited constructor: x = " + x + " and y = " + y);
}
Constructor(int i, int j) {
x = i;
y = j;
System.out.println("Both Edited constructor: x = " + x + " and y = " + y);
}
}
class Main {
public static void main(String[] args) {
Method m1 = new Method();
m1.display();
m1.display(10);
m1.display(10, 25);
Constructor c1 = new Constructor();
Constructor c2 = new Constructor(10);
Constructor c3 = new Constructor(10, 25);
}
}
Output:
Practical-7
Aim- Implementing scanner class.
Program:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.print("Enter Employe Id:");
int id = scan.nextInt();
scan.nextLine();
System.out.print("Enter Employe Name:");
String name = scan.nextLine();
System.out.print("Enter Employe Age:");
short age = scan.nextShort();
System.out.print("Enter Employe Salary:");
double salary = scan.nextDouble();
scan.close();
System.out.println("Employe Id:"+id);
System.out.println("Employe Name:"+name);
System.out.println("Employe Age:"+age);
System.out.println("Employe Salary:"+salary);
}
}
Output:
Practical-8
Aim- To check whether a given string is a palindrome or not.

Program:
import java.util.*;

class Main {
public static boolean col(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
if (col(s)) {
System.out.println("This string \"" + s + "\" is a palindrome.");
} else {
System.out.println("This string \"" + s + "\" is not a palindrome.");
}
scan.close();
}
}
Output:
Practical-8
Aim- Implementing Abstract classes.

Program:

abstract class Veh {


abstract void start();
void stop() {
System.out.println("Transport machine stopped...");
}
}
class Car extends Veh {
@Override
void start() {
System.out.println("Car started by key");
}
}
class Bike extends Veh {
@Override
void start() {
System.out.println("Bike started by kick");
}
}
public class Main {
public static void main(String[] args) {
Veh ferrari = new Car();
ferrari.start();
ferrari.stop();
Veh davidputra = new Bike();
davidputra.start();
davidputra.stop();
}
}
Output:

You might also like