0% found this document useful (0 votes)
75 views23 pages

Core-Java Practicals: 1) Quad-Eqn With User Input

The document contains code snippets and output for 5 Java programming exercises: 1) A quadratic equation solver that takes user input 2) A 2D array/matrix program that takes user input to define array size and elements 3) A string sorting program that sorts names using the compareTo() method 4) An animal package with an Animal interface and Dog and Lion classes that implement it 5) Examples of single and multilevel inheritance in Java using the extends keyword

Uploaded by

Vishal Padme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views23 pages

Core-Java Practicals: 1) Quad-Eqn With User Input

The document contains code snippets and output for 5 Java programming exercises: 1) A quadratic equation solver that takes user input 2) A 2D array/matrix program that takes user input to define array size and elements 3) A string sorting program that sorts names using the compareTo() method 4) An animal package with an Animal interface and Dog and Lion classes that implement it 5) Examples of single and multilevel inheritance in Java using the extends keyword

Uploaded by

Vishal Padme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Vishal Padme

26/07/2019

Core-Java Practicals
1)Quad-Eqn With User Input
Code:
import java.util.Scanner;
public class qe
{
public static void main(String[] args)
{
double a,b,c,r1,r2;
double determinant;
System.out.println("Enter The Value For a:");
Scanner s1= new Scanner(System.in);
a=s1.nextFloat();
System.out.println("Enter The Value For b:");
Scanner s2= new Scanner(System.in);
b=s2.nextFloat();
System.out.println("Enter The Value For c:");
Scanner s3= new Scanner(System.in);
c=s3.nextFloat();
determinant= b * b - 4 * a * c;
r1 = (-b + Math.sqrt(determinant)) / (2 * a);
r2 = (-b - Math.sqrt(determinant)) / (2 * a);
System.out.println("The Solution of eq :");
System.out.println("Root 1 :"+r1);
System.out.println("Root 2:"+r2);
}
}
Vishal Padme
26/07/2019

Output:

2)2-D Array/Matrix With M Rows And N Columns


Code:
import java.util.Scanner;
public class mtx
{
public static void main(String[] args)
{
int m,n;
System.out.println("Input M(ROWS)= ");
Scanner s=new Scanner(System.in);
m=s.nextInt();
System.out.println("Input N(COL)= ");
n=s.nextInt();
int a1[][]=new int[m][n];
int b1[][]=new int[m][n];
int c1[][]=new int[m][n];
System.out.println("Input ARRAY A Elements= ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
Vishal Padme
26/07/2019

{
Scanner s1=new Scanner(System.in);
a1[i][j]=s1.nextInt();
}
}
System.out.println("Input ARRAY B Elements= ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
Scanner s1=new Scanner(System.in);
b1[i][j]=s1.nextInt();
}
}
System.out.println("ARRAY A= ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a1[i][j]);
System.out.print(" ");
}
System.out.println();
}
System.out.println("ARRAY B= ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(b1[i][j]);
Vishal Padme
26/07/2019

System.out.print(" ");
}
System.out.println();
}
System.out.println("ARRAY ADDITION= ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
c1[i][j]=a1[i][j]+b1[i][j];
System.out.print(c1[i][j]+" ");
}
System.out.println();
}
}
}

Output:
Vishal Padme
26/07/2019

3)Sorting Of Strings Using CompareTo()

Code:
import java.util.Scanner;
public class Ns
{
public static void main(String[] args)
{
int n;
String temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter Number of Names: ");
n = s.nextInt();
String names[] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter Names ");
for(int i = 0; i < n; i++)
{
System.out.print("Name "+(i+1) +" = ");
names[i] = s1.nextLine();
}
System.out.println("Entered Names as follows:");
for (int i = 0; i < n; i++)
{
System.out.println(names[i]);
}
for (int i = 0; i < n; i++)
Vishal Padme
26/07/2019

{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}

System.out.println("Sorted Names as follows:");

for (int i = 0; i < n; i++)


{
System.out.println(names[i]);
}
}
}
Vishal Padme
26/07/2019

Output:

4)Animal Package With Implementation Of Interface Animal


i)Single Java Co. File
Code:
interface animal
{
void eat();
void sleep();
void walk();
}
class Dog implements animal
{
public void eat()
{
System.out.println("Dog Is eating");
}
Vishal Padme
26/07/2019

public void sleep()


{
System.out.println("Dog is sleeping");
}
public void walk()
{
System.out.println("Dog is walking");
}
}

class Lion implements animal


{
public void eat()
{
System.out.println("Lion Is Eating");
}
public void sleep()
{
System.out.println("Lion is sleeping");
}

public void walk()


{
System.out.println("Lion is Walking");
}
}
Vishal Padme
26/07/2019

class Animal_un
{
public static void main(String[] args)
{
Dog o1 = new Dog();
Lion o2 = new Lion();
System.out.println("Implementation Of class 1 DOG:");
o1.eat();
o1.sleep();
o1.walk();
System.out.println("Implementation Of class 2 LION:");
o2.eat();
o2.sleep();
o2.walk();
}
}

Output:

ii)With Import pkg Statement


Code:
# Animal Interface (Inside Animal1 package Folder)
package Animals1;
public interface animal1
Vishal Padme
26/07/2019

{
void eat();
void sleep();
void walk();
}

#package with implementation and main method(root dir)


import Animals1.*;
class Dog implements animal1
{
public void eat()
{
System.out.println("Dog Is eating");
}
public void sleep()
{
System.out.println("Dog is sleeping");
}
public void walk()
{
System.out.println("Dog is walking");
}
}
class Lion implements animal1
{
public void eat()
{
System.out.println("Lion Is Eating");
}
public void sleep()
{
Vishal Padme
26/07/2019

System.out.println("Lion is sleeping");
}
public void walk()
{
System.out.println("Lion is Walking");
}
}
class pack
{
public static void main(String[] args)
{
Dog o1 = new Dog();
Lion o2 = new Lion();
System.out.println("Implementation Of class 1 DOG:");
o1.eat();
o1.sleep();
o1.walk();
System.out.println("Implementation Of class 2 LION:");
o2.eat();
o2.sleep();
o2.walk();
}
}
Vishal Padme
26/07/2019

Output:

5)Java Inheritance Using ‘extends’ keyword


i)Single Inheritance
Code:
class student
{
int roll;
String name;
void prt()
{
System.out.println("Roll No= "+roll);
System.out.println("Name= "+name);
}
}
class setrecord extends student
{
setrecord(int r,String n)
{
roll=r;
name=n;
Vishal Padme
26/07/2019

}
}
public class sin
{
public static void main(String[] args)
{
setrecord o1= new setrecord(11,"Vishal");
o1.prt();
}
}

Output:

ii)Multilevel Inheritance
Code:
import java.util.*;
class student
{
int roll;
String name;
void prt()
{
System.out.println("Roll No= "+roll);
System.out.println("Name= "+name);
}
}
class setrecord extends student
{
Vishal Padme
26/07/2019

void setrecord()
{
System.out.print("Enter The Roll No= ");
Scanner s1=new Scanner(System.in);
roll=s1.nextInt();
Scanner s2=new Scanner(System.in);
System.out.print("Enter The Name = ");
name=s2.nextLine();
}
}
class result extends setrecord
{
void result()
{
setrecord();
prt();
System.out.println("Result= PASSED WITH 'D' GRADE");
}
}

public class mlein


{
public static void main(String[] args)
{
result o1= new result();
o1.result();
}
}

Output:
Vishal Padme
26/07/2019

iii)Hierarchical Inheritance
Code:
import java.util.*;
class students
{
public int r;
public String n;
public void set()
{
System.out.print("Enter The Roll No= ");
Scanner s1=new Scanner(System.in);
r=s1.nextInt();
Scanner s2=new Scanner(System.in);
System.out.print("Enter The Name ");
n=s2.nextLine();
}
public void get()
{
System.out.println("Roll No= "+r);
System.out.println("Name= "+n);
}
}
Vishal Padme
26/07/2019

class st1 extends students


{
void std1()
{
System.out.println("FAILED:");
}
}
class st2 extends students
{
void std2()
{
System.out.println("PASSED");
}
}
class hin
{
public static void main(String[] args)
{ st1 o1=new st1();
st2 o2= new st2();
o1.set();
o1.get();
o1.std1();
o2.set();
o2.get();
o2.std2();
}
}

Output:
Vishal Padme
26/07/2019

iv)Multiple Inheritance on Interfaces using Implementation


Code:
import java.util.*;
interface car
{
void dt(); //method declaration
int speed=12; //attribute(inside interface static,final)
}
interface bus
{
void speed(); //method declaration
int dt=10; //attribute(inside interface static,final)
}
class vehicle implements car,bus //impl.
{
int time=10; //common atribute(inside class default)
public void speed() //method defn
{
int speed2=(dt/time);
System.out.println("SPEED OF THE BUS="+speed2+" KMpH");
Vishal Padme
26/07/2019

}
public void dt() //method defn
{
int dt2=(speed*time);
System.out.println("Distance Travellrd By Car="+dt2+" Km");
}
}
class mulin //main fn
{
public static void main(String[] args)
{
vehicle v1=new vehicle(); //obj of impl.
v1.dt();
v1.speed();
}
}

Output:

6)Java Method Overloading & Overriding


i)Method Overloading

Code:
Vishal Padme
26/07/2019

class fu
{
void area(int a)
{
System.out.println("area of square:"+(a*a));
}
void area(int l,int b)
{
System.out.println("area of rectangle:"+(l*b));
}
void area(double r)
{
double a=(22/7)*r*r;
System.out.println("area of circle:"+a);
}
}
class mol
{
public static void main(String[] args)
{
fu o=new fu();
System.out.println("Function [1] with one int parameter");
o.area(4);
System.out.println("Function [2] with two int parameters");
o.area(5,10);
System.out.println("Function [3] with one float parameter");
o.area(5.30);
}
}

Output:
Vishal Padme
26/07/2019

ii)Method Overriding
Code:
class india
{
void scorecard(float s,float b)
{
float rate=(s/b);
System.out.println("Total Runs Scored By Team India ="+s);
System.out.println("overs Taken To Score The Runs ="+b);
System.out.println("run Rate ="+rate);
}
}
class rohit extends india
{
void scorecard(float s,float b)
{
float rate=(s/b)*100;
System.out.println("Total Runs Scored By Rohit Sharma ="+s);
System.out.println("balls Taken To Score The Runs ="+b);
System.out.println("Strike Rate ="+rate);
super.scorecard(405,50);
}
}
class ovr
{
Vishal Padme
26/07/2019

public static void main(String[] args)


{
india i=new india();
rohit o=new rohit();
o.scorecard(264,173);
}
}

Output:

7)Java Exception Handling


i)User Defined Exception
Code :
import java.util.Scanner;
class Ne extends Exception
{
Ne(String msg) //User Defined Exception Constructor_[Takes Arg. through object]
{
super(msg); // Super Class "Exception " Constructor [Arg. from Ne Constructor
passed in Super class]
}
}
class ex3
{
public static void main(String[] args)
Vishal Padme
26/07/2019

{
Scanner s=new Scanner(System.in);
System.out.print("Please Enter Your Name : ");
String name=s.nextLine();
System.out.print("Please Enter Your Age : ");
int age=s.nextInt();

try
{
if(age<18)
{
throw new Ne("Sorry "+name+ ",you are not valid to give vote");
//throwing the User Defined Exception Object with msg Arg.
}
else
{
System.out.println("Hey,"+name +" Welcome to vote ");
}
}
catch(Ne e)
{
System.out.println(e.getMessage()); //Catches the Ne[User
Defined one] Exception
}

finally
{
System.out.println("//Ballot Is Stronger Than a Bullet//"); //Finally Block
Will Get Executed In Any Case
Vishal Padme
26/07/2019

}
System.out.println("||Age Verification Completed||");
}
}

Output:

You might also like