Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
Objective: To PERFORM ALL MATHEMATICAL FUNCTIONS
Code:
import java.util.Scanner;
class Operation
{
public static void main(String args[])
{
float a,b,c;
char x;
Scanner op=new Scanner(System.in);
System.out.println("Enter a");
a=op.nextFloat();
System.out.println("Enter b");
b=op.nextFloat();
System.out.println("Enter the operation to be performed");
x=op.next().charAt(0);
switch(x)
{
case '+':
c=a+b;
System.out.println("Addition="+c);
break;
case '-':
c=a-b;
System.out.println("Subtraction="+c);
break;
case '*':
c=a*b;
System.out.println("Multiplication="+c);
break;
case '/':
c=a/b;
System.out.println("Division="+c);
break;
case '%':
if(a>b)
c=a%b;
else
c=b%a;
System.out.println("Modulo Division="+c);
break;
default:
System.out.println("Invalid operation");
}
}
}
AIM: : Write a program define a student class, describe its constructor, overload the constructor and initialize the object.
Code:
class Student
{
String uid;
String name;
float per;
int i;
Student()
{
uid="18bcs2247";
name="Sanskar Sachan";
System.out.println("UID="+uid);
System.out.println("Name="+name);
}
Student(float perc)
{
18BCS2250 Page 1
JAVA PROGRAMMING LAB
per=perc;
System.out.println("Percentage="+per);
}
}
public class Call
{
public static void main(String args[])
{
Student obj=new Student();
Student obj1=new Student(95.5f);
}
}
AIM: Write a program in java to show details of Vehicle-Car class using Inheritance as Car is a Vehicle.
Code:
class Vehicle
{
int Vehicle_no;
String type;
void info()
{System.out.println("Parent class called");
System.out.println("Vehicle is an mode of transport and it can be of many types like car,bus..");
}
int number(int num)
{
Vehicle_no=num;
return Vehicle_no;
}
}
class Car extends Vehicle
{
String type;
void info()
{
super.type="Car";
type="Sports Car";
super.info();
System.out.println("Child class called");
System.out.println("Type of vehicle="+super.type);
18BCS2250 Page 1
JAVA PROGRAMMING LAB
System.out.println(type+" is type of "+super.type);
System.out.println("Parent class is called");
System.out.println(super.type+" number="+super.number(8909));
}
}
class Inherit
{
public static void main(String args[])
{
Car obj=new Car();
obj.info();
}
}
AIM: Write 3 programs in one package to find the area of circle,Square,Triangle.Then create another package with a single class which invoke the methods of classes defined in the first package by taking input from the user.Code:
->package shape;
import java.util.Scanner;
public class calcc{
int radius;
double area;
Scanner obj=new Scanner(System.in);
public double areac() {
System.out.println("Enter radius of circle");
radius=obj.nextInt();
area= 3.14*radius*radius;
return area;
}
}
->package shape;
import java.util.Scanner;
public class calcs{
int l,b;
double area;
Scanner obj=new Scanner(System.in);
public double areas() {
System.out.println("Enter length,breadth of square");
l=obj.nextInt();
b=obj.nextInt();
area=l*b;
return area;
}
}
->package shape;
import java.util.Scanner;
import java.lang.Math;
public class calct{
int c;
double area;
Scanner obj=new Scanner(System.in);
public double areat() {
System.out.println("Enter sides of equilateral triangle");
c=obj.nextInt();
area=(Math.sqrt(3)*c*c)/4;
return area;
}
}
->package callshape;
import shape.calcc;
import shape.calcs;
import shape.calct;
public class calls {
public static void main(String[] args)
{
calc obj=new calc();
System.out.println("Area of circle="+obj.areac());
System.out.println("Area of square="+obj.areas());
System.out.println("Area of triangle="+obj.areat());
}
}
AIM: Write a program in java to implement the concept of Exception handling by creating user-defined exception.
Code:
public class excp extends Exception {
int balance;
excp(int n)
{
balance=n;
}
public String toString()
{
return "You cannot withdraw as your current balance is less than withdraw amount";
}
static void withdraw(int b,int amount)throws excp
{
if(b<amount)
throw new excp(b);
else
{
b=b-amount;
System.out.println("Your current balance="+b);
}
}
public static void main(String[] args)
{try{
withdraw(500,1000);
}
catch(excp e)
{
System.out.println("Withdraw amount exceeds current balance:"+e);
}
}
}
AIM: Write a program in java to implement the concept of overloading and overriding.
Code:
class A
{
int oper(int a,int b)
{
return a+b;
}
void override()
{
System.out.println("This is class A");
}
}
class B extends A
{
float oper(float a,float b)
{
return a*b;
}
void override()
{ System.out.println("override successfull");
System.out.println("This is class B");
}
}
public class prac8 {
public static void main(String args[])
{
B obj=new B();
System.out.println("sum="+obj.oper(2,5));
System.out.println("multilication="+obj.oper(10f,5.3f));
obj.override();
}
}
PROGRAM : 8(b)
AIM: Write a program in java to implement the concept of final class,final method,final variable through final keyword.
Code:
final class Aero
{ void write()
{
System.out.println("Final class Aero cannot be overriden");
}
}
class Bag extends Aero
{
final void override()
{
System.out.println("Final method cannot be overriden");
}
}
class C extends Bag
{
void override()
{
System.out.println("Final method cannot be overriden");
}
}
public class use {
public static void main(String[] args)
{
final int a=5;
{
a=6;
System.out.println("Final varible which remains constant");
}
C obj=new C();
Aero an=new Aero();
an.write();
obj.override();
}
}
AIM: Write a program in java read data from file and write data in file.
Code:
import java.io.*;
public class fileh {
public static void main(String[] args) throws Exception
{
InputStreamReader f=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(f);
String name="";
System.out.println("Enter text");
name=br.readLine();
System.out.println("Name="+name);
br.close();
f.close();
File fi=new File("C:\Users\Shivangi\Desktop\cu1.txt");
FileWriter fw=new FileWriter(fi);
fw.write(name);
fw.close();
FileReader fr=new FileReader("C:\Users\Shivangi\Desktop\cu1.txt");
BufferedReader b=new BufferedReader(fr);
int i;
System.out.print("Content in file=");
while((i=b.read())!=-1)
{
System.out.print((char)i);
}
fr.close();
b.close();
}
}
AIM Write a program using JDBC to insert, create, delete and update a table.
Code:
import java.sql.*;
public class rdbc{
public static void main(String args[]) throws Exception{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
//step2 create the connection object
Connection con= DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","details","7894");
//step3 create the statement object
Statement stmt=con.createStatement();
//step4 execute query
//4.1)create
String creates="create table teacher1(id int,name varchar(30),address varchar(35))";
stmt.execute(creates);
System.out.println("Created table");
//4.2)isertion
String s1="insert into teacher1 values(1,'aaa','#1')";
stmt.executeUpdate(s1);
System.out.println("Inserted");
String s2="insert into teacher1 values(2,'bbb','#2')";
stmt.executeUpdate(s2);
System.out.println("Inserted");
String s3="insert into teacher1 values(3,'ccc','#3')";
stmt.executeUpdate(s3);
System.out.println("Inserted");
String s4="insert into teacher1 values(4,'ddd','#4')";
stmt.executeUpdate(s4);
System.out.println("Inserted");
String s5="insert into teacher1 values(5,'eee','#5')";
stmt.executeUpdate(s5);
System.out.println("Inserted");
ResultSet rs = stmt.executeQuery("select * from teacher1");
while(rs.next()){
System.out.println(rs.getString(1) + " " + rs.getString(2)+" " +rs.getString(3) ); }
//4.3)updation
String k="aLTER TABLE teacher1 ADD house varchar(100)";
stmt.execute(k);
System.out.println("UPDATED");
//4.4)Deletion
String s = "DELETE FROM teacher1 WHERE id = 5";
stmt.executeUpdate(s);
System.out.println("deleted");
//4.5)display
ResultSet rs1 = stmt.executeQuery("select * from teacher1");
while(rs1.next()){
System.out.println(rs1.getString(1) + " " + rs1.getString(2)+" " +rs1.getString(3)+" " +rs1.getString(4) ); }
//step5 close the connection object
con.close();
}
}
![]() |
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team