Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am utilizing Java 8 & Netbeans. The books i am using are Java Programming 8th

ID: 3889280 • Letter: I

Question

I am utilizing Java 8 & Netbeans. The books i am using are Java Programming 8th Edition by Joyce Farrell, and
Java in a Nutshell. By Evans. O’Reilly. 6th Edition. I am not familiar with VarChar as a data type
and am a bit confused by how to populate the table. Any assistance is appreciated.

1. Create a program and Class called "calcs" to save calculations

2. Program must create a table called calcs

3. must accept 2 double type variable as input from the user

4. Option to *, / ,-, + the 2 numbers

5. must store inputted numbers as double type variables into Calcs Table

6. Display values from user in the table

Explanation / Answer

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

public class Calcs {

   private double op1;
   private double op2;
   private String optr;
  
  
    public Calcs(double op1, double op2, String optr) {
       super();
       this.op1 = op1;
       this.op2 = op2;
       this.optr = optr;
    }  
  
  
    public double calculate(){
          
       if(optr.equals("+")){
           return op1+op2;
       }
       else if(optr.equals("-")){
           return op1-op2;
       }
       else if(optr.equals("*")){
           return op1*op2;
       }
       else if(optr.equals("/")){
           return op1/op2;
       }
       else{
           System.out.println("invalid operator: "+optr);
       }
  
           return 0.0;
       }


   public static void main(String[] args) {

  
       try{
           //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","system","orcl");
      
                  
       Scanner scn=new Scanner(System.in);
      
       System.out.println("Enter 2 numbers: ");
      
       double op1=scn.nextDouble();
       double op2=scn.nextDouble();
       System.out.println("Enter Operator Option to *, / ,-, + ");
       String optr=scn.next();
      
       Calcs cal=new Calcs(op1, op2, optr);
        double result=cal.calculate();
      
        PreparedStatement pstmt=con.prepareStatement("insert into calculations values(?,?,?,?)");
        pstmt.setDouble(1,op1); //1 specifies the first parameter in the query
        pstmt.setString(2,optr);
        pstmt.setDouble(3,op2);
        pstmt.setDouble(4,result);
        int i=pstmt.executeUpdate();
        System.out.println(i+" records inserted successfully");
      

      //step3 create the statement object
        Statement stmt=con.createStatement();
        
        //step4 execute query
        ResultSet rs=stmt.executeQuery("select * from calculations");
        System.out.println("operand1"+" "+"operator"+" "+"operand 2"+"   =     "+"result");
        while(rs.next())
        System.out.println(" "+rs.getDouble(1)+"        "+rs.getString(2)+"        "+rs.getDouble(3)+"     =     "+rs.getDouble(4));
        
        //step5 close the connection object
        con.close();
        
  
       }catch(Exception e){ System.out.println(e);}
      
    }

}

/* output:-

Enter 2 numbers:
9 5
Enter Operator Option to *, / ,-, +
-
1 records inserted successfully
operand1 operator operand 2   =     result
2.0        +        3.0     =     5.0
2.0        *        3.0     =     6.0
9.0        -        5.0     =     4.0


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote