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

Java lab

Uploaded by

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

Java lab

Uploaded by

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

Bangladesh University

PROJECT
Course Title: Programming Language
(Java) Lab Course Code: CSE-2102

Prepared for: Pabon

Shaha Lecturer

Department of Computer Science &

Engineering Bangladesh University

Prepared by: Jannatul Nayim Miti

Batch CSE-63 (BSc) Day, ID: 2022 210


63001

Department of Computer Science &

Engineering Bangladesh University

Date of Submission: August 14, 2023


Real World Example:
Account

Object in JAVA:
• An entity that has state and behavior is known as an object.

• e.g., chair, bike, marker, pen, table, car, etc.

• It can be physical or logical (tangible and intangible).

• The example of an intangible object is the banking system.

Class in JAVA:
• A class is a group of objects which have common properties.

• It is a template or blueprint from which objects are created.

• 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

the behavior of an object.

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

code grouped together to perform a certain task or operation.

• It is used to achieve the reusability of code.

• It also provides the easy modification.

• and readability of code, just by adding or removing a chunk of code.

• The most important method in Java is the main() method.

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:

• Single-word method name: sum(), area()

• Multi-word method name: areaOfCircle(), stringComparision()

Types of Method:
There are two types of methods in Java:

I. Predefined Method

II. User-defined 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;

public class Calculator {


double addition(double a, double
b){ return a+b;
}
double subtraction(double a, double
b){ return a-b;
}
double multiplication(double a,double
b){ return a*b;
}

double mod(double a,double b){

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

Terms used in Inheritance:


• Class: A class is a group of objects which have common properties. It is a template or blueprint from
which objects are created.

• 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{

16. public static void main(String[] args) {


17. webDevloper d=new webDevloper(); System.out.println("Devloper salary="+d.sa
18.
19.
20.
21.
22. }
23. } 24.

Output:
Hierarchical Inheritance: In hierarchical inheritance, multiple subclasses extend from a
single superclass.

For example:

Implementation of Hierarchical Inheritance:

package javaapplication5;

class Employee3 {
float
salary=10000;

}
class programmer extends
Employee3{ float
bonusforprog=5000;

}
class webdevloper extends
Employee3 { float
bonusfordev=5500;

public class hierarchicalInhertance {


public static void main(String[] args)
{ webdevloper d=new webdevloper();
programmer p =new programmer();
System.out.println("Salary="+d.salar
y);
Output:

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:

Implementation of Hybrid Inheritance:


package javaapplication5;
class C 3. {
4.public void disp() 5.{
6.System.out.println("C"); 7.}
8. }
9.
10. class A extends C
11. {
12.public void disp()
13.{
14.System.out.println("A");
15.}
16. } 17.
18. class B extends C
19. {
20.public void disp()
21.{
22.System.out.println("B");
23.}
24.
25. } 26.
27. class D extends A
28. {
29.public void disp()
30.{
31.System.out.println("D");
32. }
33. public static void main(String args[]){
34.
35. D obj = new D();
36. A A_obj =new A();
37.
38. obj.disp();
39. A_obj.disp();
40.
41. }
42. }
43.

Output:
Method Overloading and
Overriding

Method Overloading in JAVA:


If a class has multiple methods having same name but different in parameters , it is known as Method
Overloading.

Different ways to overload the method:


There are two ways to overload the method in java

 By changing number of arguments


 By changing the data type

Implementation of Method Overloading by changing data type of


arguments:
In this example, we have created two methods that differs in data type. The first display method receives
one integer arguments and second display method receives string arguments.

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:

Can we overload java main() method:


Yes,we can overload java main method by method overloading. We can have any number of main
methods in a class by method overloading. But JVM calls main() method which receives string array as
arguments only.

Method Overriding in Java:


If subclass (child class) has the same method as declared in the parent class, it is known as method
overriding in Java.

Usage of Java Method Overriding:


• Method overriding is used to provide the specific implementation of a method which is already
provided by its superclass.

• Method overriding is used for runtime polymorphism.

Rules for Java Method Overriding:


 The method must have the same name as in the parent class.
 The method must have the same parameter as in the parent class.
 There must be an IS-A relationship (inheritance).

Implementation of Method Overriding:


In this example, we have defined the display method in the subclass as defined in the parent class but it
has some specific implementation. The name and parameter of the method are the same, and there is
IS- A relationship between the classes, so there is method overriding.
package javaapplication5; class Student{
public void displayinfo(){ System.out.println("I am a stdudent.");
}
}
class Myself extends Student{ public void displayinfo(){
System.out.println("Myself Miti.");

}
}
public class MethodOverriding {
public static void main(String[] args) { Myself m =new Myself(); m.displayinfo();
}

Output:

You might also like