University grading system maintains a database called \"GradeProcessing\" that c
ID: 3742655 • Letter: U
Question
University grading system maintains a database called "GradeProcessing" that contains a number of tables to store, retrieve and manipulate student marks. ‘Java2’ is one of the tables which contains following information/attributes for all the students enrolled in "Java2":
“ID”, “StudentName”, “Quiz”, “A1”, “A2”, “A3”, “Exam”, “Results” and “Grade”.
A sample of the table may look like as follows:
Write a JAVA Graphical User Interface (GUI) program that would perform following tasks:
Create Table: Create a table that is capable to store above information.
Insert Record: If the user of your program wants to insert a record, your program should ask for all the fields of the record and insert them in the table.
Search: The user of your program should be able to search for a particular record by ID or any other field.
Update: The user of your program should be able to update any field/s of a particular record. The record in which the update operation needs to be done will be selected by ID.
Calculate Results: The results of a student should be calculated using the following formula: Results = (Quiz * 0.05)+(A1* 0.15) +(A2* 0.2) + (A3* 0.10) + (Exam * 0.5)
Calculate Grade:
HD: Results>=85
DI: 75<=Results<85
CR: 65<=Results<75
PS: 50<=Results<65
FL: Results<50
Note that ID must be 8-digit number, A1, A2, A3 and Exam Marks must be between 0 and 100 (inclusive), and Results must be floating-point numbers with two decimal places.
Results 91.75 75.5 Grade HD DI StudentName Quiz 100 100 A1 85 60 ID A2 100 80 90 80 Exam 90 75Explanation / Answer
import java.util.*;
import java.sql.*;
public class GradingSystem {
static Scanner sc;
static boolean isTable;
static Connection con;
static Statement st;
static ResultSet rs;
static String query;
public static void createTable()
{
if(isTable)
{
System.out.println("Table is already present ");
return;
}
try{
query = "create table ITC521(StudentID char(8),StudentName char(50),Quiz float,Assignment1 float,Assignment2 float,Assignment3 float,Exam float,Results float,Grade char)";
st = con.createStatement();
rs = st.executeQuery(query);
rs.close();
st.close();
isTable=true;
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void insert()
{
System.out.println("Enter the student ID");
String id = sc.next();
System.out.println("Enter Student Name");
String name = sc.next();
System.out.println("Enter the Quiz field");
float quiz = sc.nextFloat();
System.out.println("Enter the marks of Assignment 1");
float asg1 = sc.nextFloat();
System.out.println("Enter the marks of Assignment 2");
float asg2 = sc.nextFloat();
System.out.println("Enter the marks of Assignment 3");
float asg3 = sc.nextFloat();
System.out.println("Enter the Exam marks");
float ex = sc.nextFloat();
System.out.println("Enter the result");
float result = sc.nextFloat();
System.out.println("Enter the grade");
String grade = sc.next();
try{
query = "insert into ITC521 values(id,name,quiz,asg1,asg2,asg3,ex,result,grade)";
st = con.createStatement();
rs = st.executeQuery(query);
System.out.println("Record successfully inserted ");
rs.close();
st.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void search()
{
System.out.println("To search by the field, please enter the right number StudentID -> 1 StudentName -> 2 Quiz -> 3 Assignment1 -> 4 Assignment2 -> 5 Assignment3 -> 6 Exam -> 7Results -> 8Grade -> 9");
int n = sc.nextInt();
String temp1="";
float temp2=0;
if(n==1 || n==2 || n==9)
temp1 = sc.nextLine();
else
temp2 = sc.nextFloat();
try{
query = "select * from ITC521";
st = con.createStatement();
rs = st.executeQuery(query);
System.out.println("StudentID StudentName Quiz Assignment1 Assignment2 Assignment3 Exam Results Grade");
while(rs.next())
{
if(n==1 || n==2 || n==9)
{
if(rs.getString(n).equals(temp1))
{
int i;
System.out.print(rs.getString(1) + " " + rs.getString(2) + " ");
for(i=3;i<=8;i++)
System.out.print(rs.getFloat(i) + " ");
System.out.print(rs.getFloat(9));
System.out.println();
}
}
else
{
if(rs.getFloat(n) == temp2)
{
int i;
System.out.print(rs.getString(1) + " " + rs.getString(2) + " ");
for(i=3;i<=8;i++)
System.out.print(rs.getFloat(i) + " ");
System.out.print(rs.getFloat(9));
System.out.println();
}
}
}
rs.close();
st.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void update()
{
System.out.println("Enter the student id whose record is to be updated ");
int id=sc.nextInt();
System.out.println("To search by the field, please enter the right number studentID -> 1 StudentName -> 2 Quiz -> 3 Assignment1 -> 4 Assignment2 -> 5 Assignment3 -> 6 Exam -> 7Results -> 8Grade -> 9");
int n = sc.nextInt();
String temp1="";
float temp2=0;
String[] arr={"StudentID","StudentName","Quiz","Assignment1","Assignment2","Assignment3","Exam","Results","Grade"};
if(n==1 || n==2 || n==9)
temp1 = sc.nextLine();
else
temp2 = sc.nextFloat();
try{
if(n==1 || n==2 || n==9)
query = "update ITC521 set "+arr[n-1]+"="+temp1+" where StudentID="+id;
else
query = "update ITC521 set "+arr[n-1]+"="+temp2+" where StudentID="+id;
st = con.createStatement();
st.executeUpdate(query);
rs.close();
st.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static double CalculateResult()
{
try
{
System.out.println("Enter the student id whose record is to be updated ");
int id=sc.nextInt();
query = "select * from ITC521 where StudentID="+id;
st = con.createStatement();
rs = st.executeQuery(query);
double result = rs.getFloat(3)*0.05 + rs.getFloat(4)*0.15 + rs.getFloat(5)*0.2 + rs.getFloat(6)*0.1 + rs.getFloat(7)*0.5;
rs.close();
st.close();
return result;
}
catch(Exception e)
{
e.printStackTrace();
}
return 0;
}
public static void CalculateGrade(Double result)
{
if(result >= 85)
System.out.println("HD");
else if(result >= 75 && result < 85)
System.out.println("DI");
else if(result >= 65 && result < 75)
System.out.println("CR");
else if(result >= 50 && result < 65)
System.out.println("PS");
else if(result >= 75 && result < 50)
System.out.println("FL");
}
public static void main(String[] args)
{
sc = new Scanner(System.in);
try
{
Class.forName("com.mysql.jdbc.Driver");
// assuming the name of database is Grade
Connection con=DriverManager.getConnection("jdbc:mysql://GradeProcessing/Grade","root","root");
while(true)
{
System.out.println("For the following operations 1. Create Table 2. Insert Record 3. Search 4. Update 5. Calculate Result 6. Calculate Grade Enter the option number to perform them");
int n=sc.nextInt();
double result;
if(n==1)
createTable();
else if(n==2)
insert();
else if(n==3)
search();
else if(n==4)
update();
else if(n==5)
result = CalculateResult();
else if(n==6)
{
result=CalculateResult();
CalculateGrade(result);
}
System.out.println("If you want to perform another operation press 1, else press 0 ");
int op=sc.nextInt();
if(op!=1)
break;
}
con.close();
}
catch(SQLException | ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.