Java.docx
Document Details
Uploaded by CheerfulExuberance7373
Full Transcript
**Name: Pranjal A Ojha** **Class: SYCS** **Roll No: 5255** **Practical 1** **Aim: Write a program to create a class and object in Java.** - **Class in Java:** A class in Java is a set of objects which shares common characteristics/ behaviour and common properties/ attributes. It is a user-de...
**Name: Pranjal A Ojha** **Class: SYCS** **Roll No: 5255** **Practical 1** **Aim: Write a program to create a class and object in Java.** - **Class in Java:** A class in Java is a set of objects which shares common characteristics/ behaviour and common properties/ attributes. It is a user-defined blueprint or prototype from which objects are created. For example, Student is a class while a particular student named Ravi is an object. - **Object in Java:** An object in Java is a basic unit of Object-Oriented Programming and represents real-life entities. Objects are the instances of a class that are created to use the attributes and methods of a class. A typical Java program creates many objects, which as you know, interact by invoking methods. 1. Create a class with the functions getdata to take the data from the user to calculate the simple interest and a function to display the output. **Source Code:** import java.util.Scanner; class SimpleIntr { Scanner sc; int p,n; float r,si; void getData() { sc=new Scanner(System.in); System.out.println(\"Enter the principal amount: \"); p=sc.nextInt(); System.out.println(\"Enter Number of years: \"); n=sc.nextInt(); System.out.println(\"Enter the rate of interest: \"); r=sc.nextFloat(); } void display() { si=(p\*r\*n)/100; System.out.println(\"The Simple Interest is: \"+si); } public static void main(String arg\[\]) { SimpleIntr s = new SimpleIntr(); s.getData(); s.display(); } } **Output:** **Practical 2** **Aim: Demonstrate various java inheritance using extends keyword.** **Source code:** // Single inheritance example class A { void methodA() { System.out.println(\"Method from class A\"); } } // Multilevel inheritance example class B extends A { void methodB() { System.out.println(\"Method from class B\"); } } // Hierarchical inheritance example class C extends A { void methodC() { System.out.println(\"Method from class C\"); } } class D extends B { void methodD() { System.out.println(\"Method from class D\"); } } public class InheritanceDemo { public static void main(String\[\] args) { // Single inheritance A a = new A(); a.methodA(); // Multilevel inheritance D d = new D(); d.methodA(); d.methodB(); d.methodD(); // Hierarchical inheritance B b = new B(); b.methodA(); b.methodB(); C c = new C(); c.methodA(); c.methodC(); } } **Output:** ![](media/image2.png) **Practical 3** **Aim: Demonstrate polymorphism (Overriding) in java.** 1. Write a java program to create a class called shape with a method called getArea. Create a sub-class called rectangle that overrides the getArea method to calculate the area of rectangle. **Source code:** class Shape { public int getArea(int l, int b) { return(0); } } class Rectangle extends Shape { public int getArea(int l, int b) { return(l\*b); } } public class Areaofrec { public static void main(String\[\] args) { Rectangle r = new Rectangle(); System.out.println(\"Area of rectangle is :\"+r.getArea(12,13)); } } **Output:** 2. Write a java program to create a class known as BankAccount with methods called deposit and withdraw. Create a sub-class called SavingAccount the overrides the withdraw method to prevent withdrawals if the account balance falls below 100. **Source code:** import java.util.Scanner; class BankAccount { int amt=1000; public void Deposit(int d) { amt+=d; System.out.println(\"balance: \"+amt); } public void Withdraw() {} } class SavingAccount extends BankAccount { int Balance,wa; Scanner sc; public void Withdraw() { Balance=amt; sc=new Scanner(System.in); if(Balance\