Core-Java Practicals: 1) Quad-Eqn With User Input
Core-Java Practicals: 1) Quad-Eqn With User Input
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:
{
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
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;
}
Output:
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:
{
void eat();
void sleep();
void walk();
}
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:
}
}
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");
}
}
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
Output:
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:
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
Output:
{
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: