Student Name : Shoaib Akhtar
Roll NO: SP19M2BA049
Program: BSCS 3rd smester spring
Question no 1::
package cost;
public class mainclass {
public static void main(String[] args) {
Rectangular myobj=new Rectangular();
System.out.println(myobj.h);
Rectangular sj=new Rectangular();
System.out.println(myobj.w);
}
package cost;
import java.io.*;
import java.util.Scanner;
public class Rectangular {
public static void main(String[] args) {
float a;
int h,w;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the height of the Rectangular");
h=sc.nextInt();
System.out.println("Enter the width of the Rectangular");
w=sc.nextInt();
a=(h*w);
System.out.println("Area of the Rectangular is"+a);
}
package cost;
public class cost extends Rectangular {
{
int a=100;
if(a>10) {
System.out.println("Rectangular cost is 100");
}
else {
System.out.println("Rectangular cost is 20");
}
}
}
Output::
Enter the height of the Rectangular
45
Enter the width of the Rectangular
36
Area of the Rectangular is1620.0
Question no 2::
import java.util.*;
public class Tester
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
// Declare variables
int loopDecision, xC, yC, rC;
// Loop statement
System.out.println("Enter 1 to insert your own values. " +
"Enter 2
to use preset values.");
loopDecision = console.nextInt();
/* If 1 is chosen then it goes into the if loop.
* In the if loop the user will be able to set their
* own coordinates and radius. They will then get the
* area of the circle and the circumference. */
if(loopDecision == 1)
{
// Ask user to input values
System.out.println("Enter a number 1-10 for the X
coordinate.");
xC = console.nextInt();
System.out.println("Enter a number 1-10 for the Y
coordinate.");
yC = console.nextInt();
System.out.println("Enter the radius.");
rC = console.nextInt();
System.out.println();
// Set the Circle class.
Circle p = new Circle(xC, yC, rC);
// Display their entered values
System.out.println("X coordinate = " + p.getX());
System.out.println("Y coordinate = " + p.getY());
System.out.println();
// These two for loops display the values on the side
and bottom
for(int i = 10; i > 0; i--)
{
System.out.println(i);
}
for(int i = 1; i <= 10; i++)
{
System.out.print(" " + i);
}
System.out.println();
System.out.println("Radius = " + p.getR());
// Display the area and circle
System.out.println("Area of the circle = " + p.area());
System.out.println("Circumference of the circle = " +
p.circumference());
}
/* In the else loop the user will be able to see
* the varius functions of the program. Using preset
* values assigned by me. */
else
{
// Set the values
Circle p = new Circle(10, 3, 5);
System.out.println();
// Display the starting values
System.out.println("Initial x-coordinate = " +
p.getX());
System.out.println("Initial y-coordinate = " +
p.getY());
System.out.println();
// These two for loops display the values on the side
and bottom
for(int i = 10; i > 0; i--)
{
System.out.println(i);
}
for(int i = 1; i <= 10; i++)
{
System.out.print(" " + i);
}
System.out.println();
System.out.println("Initial radius = " + p.getR());
System.out.println();
System.out.println("Now changing the values.");
System.out.println();
// Set new values
p.setX(5);
p.setY(10);
p.setR(4);
// Display the new values
System.out.println("X coordinate = " + p.getX());
System.out.println("Y coordinate = " + p.getY());
System.out.println();
// These two for loops display the values on the side
and bottom
for(int i = 10; i > 0; i--)
{
System.out.println(i);
}
for(int i = 1; i <= 10; i++)
{
System.out.print(" " + i);
}
System.out.println();
System.out.println();
System.out.println("Radius = " + p.getR());
// Show area and circumference with the new values
System.out.println("Area of the circle = " + p.area());
System.out.println("Circumference of the circle = " +
p.circumference());
}
}
}
class Point
{
// Declare variables
private double x;
private double y;
// Empty constructor
public Point()
{
}
// Constructor with variables
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
// Used to set the new x-coordinate
public void setX(double x)
{
this.x = x;
}
// Used to set the new y-coordinate
public void setY(double y)
{
this.y = y;
}
// Used to display the x-coordinate
public double getX()
{
return x;
}
// Used to display the y-coordinate
public double getY()
{
return y;
}
}
Question no 3::
package person1;
import java.util.Scanner;
public class person1 {
char name[]=new char[100],gender[]=new char[10];
int age;
Scanner sc=new Scanner(System.in);
protected void getdata()
{
System.out.println("Enter Name of the person");
gets(name);
System.out.println("Enter age of the person");
age=sc.nextInt();
System.out.println("Enter gender");
gets(gender);
}
protected void display()
{
System.out.println(+name);
System.out.println(+age);
System.out.println(gender);
}
}
}
public static void main(String[] args) {
getdata();
display();
package person1;
public class Employee extends person1{
char company[]=new char [100];
float salary;
void getdata() {
System.out.println("name of company");
gets(company);
System.out.println("Salary in RS");
salary=sc.nextInt();
}
void dispalay()
{
System.out.println(+company);
System.out.println(+salary);
}
}
package person1;
public class programmer {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee s=new Employee();
s.getdata();
s.display();
}
Question No 4
In a bank, different customers having saving account. Some customers may have taken a loan from the
bank. So bank always maintains information about bank depositors and borrowers. Design a Base class
Customer (name, phone-number).Derive a class Depositor (accno, balance) from Customer. Again derive
a class Borrower (loan-no, loan-amt) from Depositor. Write necessary member functions to read and
display the details of ‘n’ customers.
import java.sql.*;
import java.io.*;
import javax.sql.*;
class Slip2
public static void main(String args[])
Connection con;
Statement state;
ResultSet rs;
int ch;
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
con=DriverManager.getConnection("jdbc:odbc:mydsn");
System.out.println("Statement object created");
do
System.out.println("\n");
System.out.println("Menu:");
System.out.println("1.Insert Record into the Table");
System.out.println("2.Update The Existing Record.");
System.out.println("3.Display all the Records from the Table");
System.out.println("4.Exit");
System.out.println("Enter your choice: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ch=Integer.parseInt(br.readLine());
switch(ch)
case 1:
System.out.println("Enter Employee Number: ");
int no=Integer.parseInt(br.readLine());
System.out.println("Enter Employee Name: ");
String name=br.readLine();
System.out.println("Enter Employee Salary: ");
int sal=Integer.parseInt(br.readLine());
String sql="insert into emp values(?,?,?)";
PreparedStatement p=con.prepareStatement(sql);
p.setInt(1,no);
p.setString(2,name);
p.setInt(3,sal);
p.executeUpdate();
System.out.println("Record Added");
//p.close();
//con.close();
break;
case 2:
System.out.println("Enter Employee Number for the record you wish to Update: ");
no=Integer.parseInt(br.readLine());
System.out.println("Enter new Name: ");
name=br.readLine();
System.out.println("Enter new Salary: ");
sal=Integer.parseInt(br.readLine());
sql="update emp set Name=?, Salary=? where Code=?";
p=con.prepareStatement(sql);
p.setString(1,name);
p.setInt(2,sal);
p.setInt(3,no);
p.executeUpdate();
System.out.println("Record Updated");
//p.close();
//con.close();
break;
case 3:
state=con.createStatement();
sql="select * from emp";
rs=state.executeQuery(sql);
while(rs.next())
System.out.println("\n");
System.out.print("\t" +rs.getInt(1));
System.out.print("\t" +rs.getString(2));
System.out.print("\t" +rs.getInt(3));
break;
case 4:
System.exit(0);
default:
System.out.println("Invalid Choice");
break;
}while(ch!=4);
}catch(Exception e)
System.out.println(e);