You are on page 1of 3

Name: Garvit Arora

Reg No: 19BCE0422

QUESTION 1:

Develop an interface ‘Shape’ with suitable methods to set the number of sides of
the shape and calculate the area of the shape. Create a class ‘Square’ using Shape
and add circumference calculation to the Square class. Then inherit a new class
‘Cube’ from Square by adding the volume calculations. Write a Java program to
test them
CODE:
package prj;

interface Shape{
void no_of_side();
void area();
}
class square implements Shape
{
int a = 0;
double ar = 0;
@Override
public void no_of_side()
{
a = 5;
}
@Override
public void area()
{
ar = 4 * a;
System.out.println("Circumference of Square:"+ar);
}
}
class cube extends square
{
int l = 0;
double ar;
public void input()
{
super.no_of_side();
l = 5;
}
public void volume()
{
super.area();
ar = l * l * l;
System.out.println("Volume of Cube:"+ar);
}
}
public class labfat {
public static void main(String[] args)
{
cube obj = new cube();
obj.input();
obj.volume();
}
}

OUTPUT:

QUESTION 2:

Write a simple Java program which connects to the underlying student database
created in MySQL using JDBC objects to display the student’s details.
CODE:
package javaDB;
import java.sql.*;
public class javadb {

public static void main(String[] args) {


try (
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/STUDENT","myuser", "root");
Statement stmt = conn.createStatement();
) {
String strSelect = "select id, name, age, marks from student";
System.out.println("The SQL statement is: " + strSelect + "\n");
ResultSet rset = stmt.executeQuery(strSelect);
System.out.println("The records selected are:");
int rowCount = 0;
while(rset.next()) { // Move the cursor to the next row, return
false if no more row
int id = rset.getInt("Id");
String name = rset.getString("Name");
int age = rset.getInt("Age");
int marks = rset.getInt("Marks");
System.out.println(id + ", " + name + ", " + age + ", " +
marks);
++rowCount;
}
System.out.println("Total number of records = " + rowCount);

} catch(SQLException ex) {
ex.printStackTrace();
}
}
}

OUTPUT

You might also like