Programs on java library
1)
public class Book {
public static void main(String[] args) {
Book b1 = new Book();
[Link]([Link]());
[Link](b1);//[Link]()
[Link](new Book());
}
}
Output:
[Link]@3fee733d
[Link]@3fee733d
[Link]@5acf9800
2)
public class Student {
@Override
public String toString()
{
return "Student1234";
}
public static void main(String[] args) {
Student s1 = new Student();
[Link](s1);//implicit call to toString()
[Link]([Link]());//explicit call to toString()
Student s2 = new Student();
[Link](s2);
}
}
Output:
Student1234
Student1234
Student1234
3)
public class Employee {
int id;
String name;
public Employee(int id, String name) {
[Link] = id;
[Link] = name;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + "]";
}
public static void main(String[] args) {
Employee emp1 = new Employee(101,"Smith");
[Link](emp1);
Employee emp2= new Employee(102,"Allen");
[Link](emp2);
}
}
Output:
Employee [id=101, name=Smith]
Employee [id=102, name=Allen]
4)
public class Student {
int age;
String name;
public Student(int age, String name) {
[Link] = age;
[Link] = name;
}
@Override
public String toString()
{
return "Student "+name+" age is "+ age;
}
}
public class MainStudent {
public static void main(String[] args) {
Student s1 = new Student(21,"Ritu");
Student s2 = new Student(22,"Ram");
Student s3 = new Student(23,"Aman");
Student[] s = new Student[3];
s[0]=s1;
s[1]=s2;
s[2]=s3;
for(int i=0 ; i<[Link] ; i++)
{
// [Link](s[i].age +" "+s[i].name);
[Link](s[i]);
}
}
}
Output:
Student Ritu age is 21
Student Ram age is 22
Student Aman age is 23
5)
public class Laptop {
String brand;
int cost;
String ramsize;
Laptop(String brand,int cost,String ramsize)
{
[Link]=brand;
[Link]=cost;
[Link]=ramsize;
@Override
public String toString()
{
return "brand : "+brand+" cost : "+cost+
" ramsize "+ramsize;
}
}
public class Customer {
public static void main(String[] args) {
Laptop l1 = new Laptop("Hp",30000,"4GB");
Laptop l2 = new Laptop("Dell",40000,"8GB");
Laptop l3 = new Laptop("Lenovo",50000,"16GB");
Laptop[] l = {l1,l2,l3};
for(int i=0 ; i<[Link];i++)
{
[Link](l[i]);
}
}
}
Output:
brand : Hp cost : 30000 ramsize 4GB
brand : Dell cost : 40000 ramsize 8GB
brand : Lenovo cost : 50000 ramsize 16GB
6)
public class Employee {
// @Override
// public int hashCode()
// {
// return 1234;
// }
//
public static void main(String[] args) {
Employee e1 = new Employee();
[Link]([Link]());
Employee e2 = new Employee();
[Link]([Link]());
}
}
Output:
1072591677
1523554304
7)
public class Student {
int age;
public Student(int age) {
[Link] = age;
}
public static void main(String[] args) {
Student s1 = new Student(20);
Student s2 = new Student(20);
[Link](s1==s2);
[Link]([Link](s2));
Output:
false
false
8)
public class Student {
int age;
public Student(int age) {
[Link] = age;
}
@Override
public boolean equals(Object obj)//upcasting
{
Student s = (Student) obj;
return [Link]==[Link];
// [Link] == [Link]
// 25==20
}
public static void main(String[] args) {
Student s1 = new Student(25);
Student s2 = new Student(20);
[Link]([Link](s2));
}
Output:
false
Programs on singleton class
1)
public class Account {
private static Account obj=null;
private Account()
{
[Link]("Object created");
public static void createObject()
{
if(obj==null)
{
obj=new Account();
}
else {
[Link]("cannot create object");
}
}
}
public class User {
public static void main(String[] args) {
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Output:
Object created
cannot create object
cannot create object
cannot create object
cannot create object
cannot create object
2)
public class Marriage {
private static Marriage m=null;
private Marriage()
{
[Link]("Got married");
}
public static void createInstance()
{
if(m==null)
{
m=new Marriage();
}
else {
[Link]("already married");
}
}
}
public class Solution {
public static void main(String[] args) {
[Link]("start");
[Link]();
[Link]();
[Link]();
[Link]();
[Link]("end");
}
}
Output:
start
Got married
already married
already married
already married
end
Programs on String
1)
public class Demo1 {
public static void main(String[] args) {
String s1 ="java";
String s2 = new String("sql");
String s3 = "java";
String s4 = new String("sql");
[Link](s1==s3);
[Link](s2==s4);
[Link]([Link](s4));
}
}
Output:
true
false
true
2)
public class Car {
public static void main(String[] args) {
Car c1 = new Car();
[Link](c1);
[Link]([Link]());
[Link]("================");
[Link]([Link]());
[Link]("================");
Car c2 = new Car();
Car c3 = new Car();
[Link]([Link](c3));
}
}
Output:
[Link]@3fee733d
[Link]@3fee733d
================
1072591677
================
false
3)
public class Demo2 {
public static void main(String[] args) {
String s = new String("A");
[Link](s);
[Link]([Link]());
[Link]("================");
[Link]([Link]());
[Link]("=================");
String s1 = new String("java");
String s2 = new String("Java");
[Link]([Link](s2));
}
}
Output:
A
A
================
65
=================
False
4)
public class Test {
public static void main(String[] args) {
//empty representation of the string
String s1 = new String();
[Link](s1);
String s2 = new String("Smith");
[Link](s2);
char[] c = {'j','a','v','a'};
String s3 = new String(c);
[Link](s3);
}
}
Output:
Smith
java
5)
public class Solution {
public static void main(String[] args) {
String s1 = new String("Java");
[Link](s1);
[Link]("Program");
[Link](s1);
[Link]("==================");
String s2 = new String("Java");
[Link](s2);
s2=[Link]("Program");
[Link](s2);
[Link]("===================");
StringBuffer s3 = new StringBuffer("Good");
[Link](s3);
[Link]("Morning");
[Link](s3);
[Link]("=====================");
StringBuilder s4 = new StringBuilder("Hello");
[Link](s4);
[Link]("World");
[Link](s4);
}
}
Output
Java
Java
==================
Java
JavaProgram
===================
Good
GoodMorning
=====================
Hello
HelloWorld
6)
public class MethodsDemo {
public static void main(String[] args) {
String s = "Software Developer";
[Link]([Link]());
[Link]("========================");
[Link]([Link]());
[Link]("=======================");
[Link]([Link]());
[Link]("=======================");
[Link]([Link]("soft"));
[Link]([Link]("Soft"));
[Link]("=======================");
[Link]([Link]("er"));
[Link]([Link]("Eloper"));
[Link]("======================");
[Link]([Link]("dev"));
[Link]([Link]("Dev"));
[Link]("========================");
[Link]([Link](" in xyz"));
[Link]("=========================");
//"Software Developer"
[Link]([Link](2));
[Link]([Link](14));
[Link]("=================");
[Link]([Link]('t'));
[Link]([Link]('D'));
[Link]([Link]('r'));
[Link]("==================");
String a = "java";
String b = "JavA";
String c = "java";
[Link]([Link](b));
[Link]([Link](c));
[Link]([Link](b));
[Link]("==================");
String x = "hello dinga";
[Link]([Link](3));
[Link]([Link](7));
[Link]([Link](2,7));
[Link]([Link](4,10));
}
}
Output
18
========================
SOFTWARE DEVELOPER
=======================
software developer
=======================
false
true
=======================
true
false
======================
false
true
========================
Software Developer in xyz
=========================
f
o
=================
3
9
6
==================
false
true
true
==================
lo dinga
inga
llo d
o ding
Projects
1)
public interface Calculator {
public abstract int add(int x, int y);
public abstract int sub(int x, int y);
public abstract int mul(int x, int y);
public abstract int div(int x, int y);
String displayErrorMsg();
}
public class CalculatorImpl implements Calculator {
@Override
public int add(int x, int y) {
return x+y;
}
@Override
public int sub(int x, int y) {
return x-y;
}
@Override
public int mul(int x, int y) {
return x*y;
}
@Override
public int div(int x, int y) {
if(x!=0 && y!=0) {
return x/y;
}
return 0;
}
@Override
public String displayErrorMsg() {
return "Invalid Choice";
}
import [Link];
public class Solutions {
public static void main(String[] args) {
Scanner s = new Scanner([Link]);
Calculator calc = new CalculatorImpl();
while(true)
{
[Link]("1:Addition\n2:Subtraction\n3:Multiplication"
+ "\n4:Division\n5:Exit\n Enter choice");
int choice = [Link]();
int a=0;
int b=0;
if(choice >= 1 && choice <=4)
{
[Link]("enter 2 numbers");
a = [Link]();
b = [Link]();
}
switch(choice)
{
case 1:
[Link]("Sum of "+a+" & "+b+" is "+[Link](a, b));
break;
case 2:
[Link]("Difference of "+a+" & "+b+" is "+[Link](a,
b));
break;
case 3:
[Link]("Product of "+a+" & "+b+" is "+[Link](a, b));
break;
case 4:
if(a!=0 && b!=0) {
[Link]("Division of "+a+" & "+b+" is "+[Link](a, b));
}
else{
[Link]("Kindly do not give 0 as neumanator or"
+ " Denominator");
}
break;
case 5 :
[Link]("Thank you!!");
[Link](0);
default:
[Link]([Link]());
}
[Link]("----------------------------------");
}
}
}
Output
1:Addition
2:Subtraction
3:Multiplication
4:Division
5:Exit
Enter choice
1
enter 2 numbers
40
70
Sum of 40 & 70 is 110
----------------------------------
1:Addition
2:Subtraction
3:Multiplication
4:Division
5:Exit
Enter
choice
2)
public interface Bank {
void deposit(int amount);
void withdraw(int amount);
int getBalance();
}
public class BankImpl implements Bank {
int balance;
BankImpl(int balance)
{
[Link]=balance;
}
@Override
public void deposit(int amount) {
[Link]("depositing rs: "+amount);
balance = balance+amount;
[Link]("Amount deposited successfully");
}
@Override
public void withdraw(int amount) {
if(amount <= balance)
{
[Link]("withdrawing Rs : "+amount);
balance = balance-amount;
[Link]("Amount withdrawn successfully");
}
}
@Override
public int getBalance() {
return balance;
}
import [Link];
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Bank bank = new BankImpl(5000);
while(true)
{
[Link]("1: Deposit Amount \n 2: Withdraw Amount "
+ "\n 3: Check Balance \n 4: Exit");
[Link]("enter the choice");
int choice = [Link]();
switch(choice)
{
case 1:
[Link]("enter the amount to be deposited: ");
int amountToDeposit = [Link]();
[Link](amountToDeposit);
break;
case 2 :
[Link]("enter the amount to be withdrawn :");
int amountToWithdraw = [Link]();
[Link](amountToWithdraw);
break;
case 3 :
[Link](" Available Balance : "+[Link]());
break;
case 4 :
[Link]("Thank you !! visit again !!");
[Link](0);
default:
[Link]("Invalid choice ");
[Link]("===================================");
}
}
}
Output:
1: Deposit Amount
2: Withdraw Amount
3: Check Balance
4: Exit
enter the choice
1
enter the amount to be deposited:
5000
depositing rs: 5000
Amount deposited successfully
===================================
1: Deposit Amount
2: Withdraw Amount
3: Check Balance
4: Exit
enter the choice
3
Available Balance : 10000
===================================
1: Deposit Amount
2: Withdraw Amount
3: Check Balance
4: Exit
enter the choice
2
enter the amount to be withdrawn :
2000
withdrawing Rs : 2000
Amount withdrawn successfully
===================================
1: Deposit Amount
2: Withdraw Amount
3: Check Balance
4: Exit
enter the choice
3
Available Balance : 8000
===================================
1: Deposit Amount
2: Withdraw Amount
3: Check Balance
4: Exit
enter the choice