Java lab
Java lab
PROJECT
Course Title: Programming Language
(Java) Lab Course Code: CSE-2102
Shaha Lecturer
Object in JAVA:
• An entity that has state and behavior is known as an object.
Class in JAVA:
• A class is a group of objects which have common properties.
• It is a logical entity.
• It can't be physical.
Method in JAVA:
In Java, a method is like a function which is used to expose
Real Example:
First create Java Program to demonstrate the working of a banking-system, where we deposit and
withdraw amount from our account. Creating an Account class which has deposit() and withdraw()
methods.
Input:
1. package javaapplication5; 2.
3. import java.util.Scanner; 4.
5. public class Account {
6. String acc_holder;
7. double deposit,withdraw,new_balance;
8. double balance;
9. long acc;
10. Scanner sc=new
Scanner(System.in); 11.
12. void information(){
13. System.out.print("Name of account holder:");
14. acc_holder=sc.next();
15. System.out.print("Account number:");
16. acc=sc.nextLong();
17. System.out.print("Initial Deposit:");
18. deposit=sc.nextDouble();
19. balance=deposi
t; 20.
21.
22. }
23. void showBalance(){
24. System.out.println("Your current balance is:"+balance);
25. }
26. void deposit(){
27. System.out.print("Enter the amount you deposit:");
28. double deposit_amount=sc.nextDouble();
29. balance=balance+deposit_amou
nt; 30.
31.
32. }
33. void withdraw(){
34. System.out.println("Enter the amount you withdraw:");
35. double withdraw_amount=sc.nextDouble();
36. if(withdraw_amount <=balance){
37. balance=balance-withdraw_amount;
38. }
39. else
40. {
41. System.out.println("Sorry! You don't have sufficient balance");
42. }
43.
44.
45. }
46. void accInfo(){
47. System.out.println("------Your account info- ");
48. System.out.println("Account name:"+acc_holder);
49. System.out.println("Account Number:"+acc);
50. System.out.println("Current
Balance:"+balance); 51.
52. }
53.
54. }
55. class main extends
Account{ 56.
57. public static void main(String[] args) {
58. System.out.println("Welcome to M-Bank");
59. Account Ac = new Account();
60. boolean D = true;
61. while (D)
62. {
63. System.out.println("1. Create Ac\n2. Show balance\n3. Deposit amount\n4.
Withdraw Amount\n5. Account info\n6. Exit");
64. System.out.print("Enter your choise:");
65. int n =
Ac.sc.nextInt(); 66.
67. switch(n){
68. case 1:
69. Ac.information();
70. break;
71. case 2:
72. Ac.showBalance();
73. break;
74. case 3:
75. Ac.deposit();
76. break;
77.
78. case 4:
79. Ac.withdraw();
80. break;
81. case 5:
82. Ac.accInfo();
83. break;
84. case 6 :
85. System.out.println("Thank you.\nYou are successfully done.");
86. D = false;
87. break;
88.
89. }
90.
91. }
92. }
93. }
94.
95.
Output:
Build a calculator by using class and
object in Java
Method in JAVA:
• A method is a block of code or collection of statements or a set of
Naming a Method:
• method name must be a verb and start with a lowercase letter.
• In the multi-word method name, the first letter of each word must be in uppercase except the
first word.
• For example:
Types of Method:
There are two types of methods in Java:
I. Predefined Method
Example:
Here I build a calculator by using class and object in Java. Where method are-
I. Addition
II. Subtraction
III. Multiplication
IV. Division
V. Mod
• Which take user choice and take user input from user.
Code:
package javaapplication5;
import java.util.Scanner;
return a%b;
}
void division(double a, double
b){ double res = 0;
if(b==0)
{
System.out.println("Cannot devide by zero!");
}
else
{
res = a/b;
System.out.println(""+
res);
}
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
Calculator calculator = new
Calculator();
System.out.println("Choose an
option:");
System.out.println("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5.
Modulas");
int opt=input.nextInt();
System.out.println("Enter two
numbers:"); double
n1=input.nextDouble();
double n2=input.nextDouble();
switch(opt){
case 1:
System.out.println("Result="+calculator.addition(n1, n2));
break;
case 2:
System.out.println("Result="+calculator.subtraction(n1,
n2)); break;
case 3:
System.out.println("Result="+calculator.multiplication(n1,
n2)); break;
case 4:
calculator.division(n1,n2
); break;
case 5:
System.out.println("Result="+calculator.mod(n1, n2)); break;
default:
System.out.println("Invalid Choise.");
}
}
Output:
Inheritance in JAVA
Introduction:
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a
parent object. Inheritance represents the IS-A relationship which is also known as a parent-child
relationship.
Types of Inheritance:
There are five types of inheritance.
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
• Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived
class, extended class, or child class.
• Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It
is also called a base class or a parent class.
Single Inheritance: In single inheritance, a single subclass extends from a single superclass.
• For example:
Implementation of Single Inheritance:
1. package
javaapplication5; 2.
Output:
3. public class Employee {
4. float
salary=10000; 5.
6. }
7. class Devloper extends Employee{
8. float bonus=5000;
9. public static void main(String[] args) {
10. Devloper d=new Devloper();
11. System.out.println("Devloper salary="+d.salary);
12. System.out.println("Bonus="+d.bonus);
13. }
14.
Multilevel Inheritance:
} In multilevel inheritance, a subclass extends from a superclass and
then the same subclass acts as a superclass for another class.
• For example:
Implementation of Multilevel Inheritance:
1. package javaapplication5;
2.
class Employee2 {
float salary=10000; 5.
6. }
class Programmer extends Employee2{
float bonusforprog=5000; 9.
10. }
class webDevloper extends Programmer{
float bonusfordev=5500; 13.
14. }
15. public class multilevelInheritance{
Output:
Hierarchical Inheritance: In hierarchical inheritance, multiple subclasses extend from a
single superclass.
For example:
package javaapplication5;
class Employee3 {
float
salary=10000;
}
class programmer extends
Employee3{ float
bonusforprog=5000;
}
class webdevloper extends
Employee3 { float
bonusfordev=5500;
Multiple Inheritance: In multiple inheritance, a single subclass extends from multiple super
classes.
• For example:
Multiple inheritance is not possible in JAVA. The reason behind is to prevent ambiguity. Consider a case
where class B extends class A and class C and both class A and C have the same method display().Now
java compiler cannot decide, which display method it should inherit. To prevent such situation, multiple
inheritances is not allowed in java.
Hybrid Inheritance: Hybrid inheritance is a combination of two or more types of inheritance.
• For example:
Output:
Method Overloading and
Overriding
1. package javaapplication5;
2.
3. public class MethodOverloading {
4. static void display(int a)
5. {
6. System.out.println("My self Miti.");
7. }
8. static void display(String a)
9. {
10. System.out.println("I am a student of CSE");
11. }
12. public static void main(String[] args) { display(1);
13. display("Hi");
14.
15.
16. }
17.
18. } 19.
Output:
}
}
public class MethodOverriding {
public static void main(String[] args) { Myself m =new Myself(); m.displayinfo();
}
Output: