JBA
Practical1-A
public class Student { //instance variables of the class int id; String name; Student(){ System.out.println("this a default constructor"); } Student(int i, String n){ id = i; name = n; } public static void main(String[] args) { //object creation Student s = new Student(); System.out.println("\nDefault Constructor values: \n"); System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name); System.out.println("\nParameterized Constructor values: \n"); Student student = new Student(10, "David"); System.out.println("Student Id : "+student.id + "\nStudent Name : "+student.name); } }
A.2-MethoD Overloading
class Adder{ static int add(int a,int b){return a+b;} static int add(int a,int b,int c){return a+b+c;} } class TestOverloading1{ public static void main(String[] args){ System.out.println(Adder.add(11,11)); System.out.println(Adder.add(11,11,11)); }}A.3-Static Method
class Student{ int rollno; String name; static String college = "ITS"; //static method to change the value of static variable static void change(){ college = "BBDIT"; } //constructor to initialize the variable Student(int r, String n){ rollno = r; name = n; } //method to display values void display(){System.out.println(rollno+" "+name+" "+college);} } //Test class to create and display the values of object public class TestStaticMethod{ public static void main(String args[]){ Student.change();//calling change method //creating objects Student s1 = new Student(111,"Karan"); Student s2 = new Student(222,"Aryan"); Student s3 = new Student(333,"Sonoo"); //calling display method s1.display(); s2.display(); s3.display(); } }
B.1Inheritance
class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ void bark(){System.out.println("barking...");} } class BabyDog extends Dog{ void weep(){System.out.println("weeping...");} } class TestInheritance2{ public static void main(String args[]){ BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); }}
B.2 Method Overriding
class Vehicle{ //defining a method void run(){System.out.println("Vehicle is running");} } //Creating a child class class Bike2 extends Vehicle{ //defining the same method as in the parent class void run(){System.out.println("Bike is running safely");} public static void main(String args[]){ Bike2 obj = new Bike2();//creating object obj.run();//calling method } }
PRACTICAL 2:
1A Abstract class
abstract class Shape{
abstract void draw();
}
//In real scenario, implementation is provided by others i.e. unknown by end user
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
//In real scenario, method is called by programmer or user
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();//In a real scenario, object is provided through method, e.g., getShape() method
s.draw();
}
}
PRACTICAL 2: 1B interface
interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){return 9.15f;}
}
class PNB implements Bank{
public float rateOfInterest(){return 9.7f;}
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
Practical 3
User defined exception
class InvalidAgeException extends Exception
{
public InvalidAgeException (String str)
{
// calling the constructor of parent Exception
super(str);
}
}
// class that uses custom exception InvalidAgeException
public class TestCustomException1
{
// method to check the age
static void validate (int age) throws InvalidAgeException{
if(age < 18){
// throw an object of user defined exception
throw new InvalidAgeException("age is not valid to vote");
}
else {
System.out.println("welcome to vote");
}
}
// main method
public static void main(String args[])
{
try
{
// calling the method
validate(13);
}
catch (InvalidAgeException ex)
{
System.out.println("Caught the exception");
// printing the message from InvalidAgeException object
System.out.println("Exception occured: " + ex);
}
}
}
PRACTICAL 2: 1A Abstract class
abstract class Shape{
abstract void draw();
}
//In real scenario, implementation is provided by others i.e. unknown by end user
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
//In real scenario, method is called by programmer or user
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();//In a real scenario, object is provided through method, e.g., getShape() method
s.draw();
}
}
PRACTICAL 2: 1B interface
interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){return 9.15f;}
}
class PNB implements Bank{
public float rateOfInterest(){return 9.7f;}
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
Practical 3
User defined exception
class InvalidAgeException extends Exception
{
public InvalidAgeException (String str)
{
// calling the constructor of parent Exception
super(str);
}
}
// class that uses custom exception InvalidAgeException
public class TestCustomException1
{
// method to check the age
static void validate (int age) throws InvalidAgeException{
if(age < 18){
// throw an object of user defined exception
throw new InvalidAgeException("age is not valid to vote");
}
else {
System.out.println("welcome to vote");
}
}
// main method
public static void main(String args[])
{
try
{
// calling the method
validate(13);
}
catch (InvalidAgeException ex)
{
System.out.println("Caught the exception");
// printing the message from InvalidAgeException object
System.out.println("Exception occured: " + ex);
}
}
}
PRACTICAL 2: 1A Abstract class
abstract class Shape{
abstract void draw();
}
//In real scenario, implementation is provided by others i.e. unknown by end user
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
//In real scenario, method is called by programmer or user
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();//In a real scenario, object is provided through method, e.g., getShape() method
s.draw();
}
}
PRACTICAL 2: 1B interface
interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){return 9.15f;}
}
class PNB implements Bank{
public float rateOfInterest(){return 9.7f;}
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
Practical 3
User defined exception
class InvalidAgeException extends Exception
{
public InvalidAgeException (String str)
{
// calling the constructor of parent Exception
super(str);
}
}
// class that uses custom exception InvalidAgeException
public class TestCustomException1
{
// method to check the age
static void validate (int age) throws InvalidAgeException{
if(age < 18){
// throw an object of user defined exception
throw new InvalidAgeException("age is not valid to vote");
}
else {
System.out.println("welcome to vote");
}
}
// main method
public static void main(String args[])
{
try
{
// calling the method
validate(13);
}
catch (InvalidAgeException ex)
{
System.out.println("Caught the exception");
// printing the message from InvalidAgeException object
System.out.println("Exception occured: " + ex);
}
}
}
PRACTICAL 4
List
import java.util.*; public class Main { public static void main(String args[]){ //Create List object List<String> mySubjects = new ArrayList<>(); //Add elements to list mySubjects.add("Java"); mySubjects.add("Spring"); mySubjects.add("Hibernate"); //Add elements at specified position mySubjects.add(1,"SQL"); mySubjects.add(2,"Oracle"); System.out.println("My Subjects:"); //Print all subjects for(String subject : mySubjects){ System.out.println(subject); } //Print element on 2nd index System.out.println("Element at 2nd index: "+mySubjects.get(2)); } }
Set import java.util.*; public class Main { public static void main(String args[]){ //Create Set object Set<String> mySubjects = new HashSet<>(); //Add elements to Set mySubjects.add("Java"); mySubjects.add("Spring"); mySubjects.add("Hibernate"); System.out.println("My Subjects:"); //Print all subjects for(String subject : mySubjects){ System.out.println(subject); } } } Map A map in java, not extends the Collection interface. It represents a group of special elements or objects. Every map element or object contains key and value pair. A map can’t contain duplicate keys and one key can refer to at most one value. Example import java.util.*; public class Main { public static void main(String args[]){ Map<Integer,String> mysubjects = new HashMap<Integer,String>(); mysubjects.put(1,"Java"); mysubjects.put(2,"Spring"); mysubjects.put(3,"Oracle"); for(Map.Entry subject : mysubjects.entrySet()) System.out.println(subject.getKey()+" - "+subject.getValue()); }
Practical 5import javax.swing.*; import java.awt.*; import java.awt.event.*; class Biodata extends JFrame implements ActionListener { private JLabel l1,l2,l3,l4; private JTextField n; private JRadioButton r1,r2; private ButtonGroup g; private JCheckBox c1,c2,c3; private JTextArea a; private JButton b; Biodata() { super("Enter Biodata"); Container c = getContentPane(); c.setLayout(new GridLayout(4,2)); l1 = new JLabel("Name"); c.add(l1); n = new JTextField("Enter your name",20); n.setToolTipText("Please enter your name"); c.add(n); l4 = new JLabel("Gender"); c.add(l4); r1 = new JRadioButton("Male",true); c.add(r1); r2 = new JRadioButton("Female",false); c.add(r2); g = new ButtonGroup(); g.add(r1); g.add(r2); l2 = new JLabel("Qualification"); c.add(l2); c1 = new JCheckBox("BTech"); c.add(c1); c2 = new JCheckBox("MTech"); c.add(c2); c3 = new JCheckBox("MCA"); c.add(c3); l3 = new JLabel("Address"); c.add(l3); a = new JTextArea(10,15); c.add(a); b = new JButton("Show"); c.add(b); b.addActionListener(this); setVisible(true); setSize(400,400); } public void actionPerformed(ActionEvent e) { String s = ""+n.getText()+"\n"; s += (r1.isSelected())?r1.getText()+"\n":r2.getText() +"\n"; if(c1.isSelected()) s += (c1.getText()) + " "; if(c2.isSelected()) s += (c2.getText()) + " "; if(c3.isSelected()) s += (c3.getText()); s += "\n"+a.getText()+"\n"; JOptionPane.showMessageDialog(null,s); } public static void main(String args[]) { new Biodata(); } }Practical 7
Comments
Post a Comment