Example Answer Code For JAVA (OOP) Practical Model Questions

Model Question:-

Calculate GPA for given Marks by a User.

Steps: 1. Ask User to Enter his/her Index Number 2. Again through the console ask user to enter grades 3. Input format should be validated. It should only contain two letters. First letter denote the No. of credits allocated and second letter denote your grade. E.g. 3A -> you have got ‘A’ pass for three credit subject 4. Then you should calculate the GPA of the student.

GPA = ∑(No.of credits * Grade weight) / ∑ Total No.of Credits
Grade A B C D Grade Weight 4 3 2 1

5. Print the GPA on the console. 6. Then print the values to an output file called “CalculatedGPA.txt” E.g. ================Console side=============== Please Enter Your Id: UWU/CST/07/0039 Please Enter Your Grades: 2A 3B 2C 1A Your GPA is: 3.12 ================File Side==================== Index No GPA UWU/CST/07/0039 3.12

import java.io.ObjectInputStream.GetField; import java.util.Scanner; public class GPACalculator { public static void main(String[] args) { GPACalculator gpaCalculator=new GPACalculator(); gpaCalculator.getValue(); } String ID=""; String grade=""; public void getValue(){ Scanner sc=new Scanner(System.in); System.out.println("Please insert your ID....."); this.ID=sc.nextLine(); try{ validateID(this.ID,this.grade); }catch(Exception ex){ getValue(); } enterGrade(this.ID); } public void enterGrade(String ID){ Scanner sc=new Scanner(System.in); System.out.println("Please insert your grade....."); grade=sc.nextLine(); if(grade.equals("0")){ System.out.print(this.totalGPA/this.totalCredit); System.exit(0); } validateID(ID,this.grade); } public void validateID(String ID,String grade){ String uwu=""; String iitORcst=""; String batch=""; String regNo="";

int regNoInteger=0; uwu=ID.substring(0, 3); iitORcst=ID.substring(4, 7); batch=ID.substring(8, 10); regNo=ID.substring(11, 15); regNoInteger=Integer.parseInt(regNo); if(uwu.equalsIgnoreCase("uwu")&& (iitORcst.equalsIgnoreCase("iit")|| iitORcst.equalsIgnoreCase("cst"))&&batch.equals("11")&&regNoInteger<1000){ validateGrade(grade); }else{ System.out.print("Error"); } } public void validateGrade(String grade){ int length=grade.length(); if(length==2){ String credit=grade.substring(0, 1); if(Integer.parseInt(credit)<10){ String gradeValue=grade.substring(1, 2); if(gradeValue.equals("A") || gradeValue.equals("B") || gradeValue.equals("C") || gradeValue.equals("D") || gradeValue.equals("E")){ calculateGPA(credit,gradeValue); } } } } float totalGPA=0; float totalCredit=0; public void calculateGPA(String credit, String gradeValue){ if(gradeValue.equals("A")){

this.totalGPA=this.totalGPA+Integer.parseInt(credit)*4; this.totalCredit=this.totalCredit+Integer.parseInt(credit); }else if(gradeValue.equals("B")){ this.totalGPA=this.totalGPA+Integer.parseInt(credit)*3; this.totalCredit=this.totalCredit+Integer.parseInt(credit); } else if(gradeValue.equals("C")){ this.totalGPA=this.totalGPA+Integer.parseInt(credit)*2; this.totalCredit=this.totalCredit+Integer.parseInt(credit); } else if(gradeValue.equals("D")){ this.totalGPA=this.totalGPA+Integer.parseInt(credit)*1; this.totalCredit=this.totalCredit+Integer.parseInt(credit); } enterGrade(this.ID); } }

Model Question:-

Calculate Employee Salary.

Steps: 1. Ask User to Enter his/her Employee Id 2. If it start with ‘P’ he is a Permanent Employee, if it’s ‘H’ he is a hourly based employee. Other Letters are not allowed (This should be validated) 3. Then ask him to enter No.of Hours worked. 4. Then calculated his total salary. 5. For one hour Permanent Employee receives Rs.750 and Hourly Based Employee receive Rs.1000. 6. For Permanent employees who has worked more than 200 hrs they receive additional Rs. 750 and For Hourly based employees who has worked more than 250 hrs They receive additional Rs. 500. 7. Print the Salary on the console. 8. Then print the values to an output file called “CalculatedSalary.txt”

import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; public class Salary { String empID=""; public static void main(String[] args) { Salary salary=new Salary(); salary.loggingForm(); } public void loggingForm(){ Scanner sc=new Scanner(System.in); System.out.println("Please Enter Your Employee ID......."); empID=sc.nextLine(); validation(this.empID); } public void validation(String empID){ char position=empID.charAt(0); if(position=='P' || position=='H'){ setSalary(position); }else{ System.out.println("Invalidate Employer"); } } public void setSalary(char position){ Scanner sc=new Scanner(System.in); int workingHours=0; double salary=0; System.out.println("Enter the working hours...."); workingHours=sc.nextInt(); if(position=='P'){ salary=salary+750*workingHours; if(workingHours>200){ salary=salary+750;

} }else if(position=='H'){ salary=salary+1000*workingHours; if(workingHours>250){ salary=salary+500; } } publishSalary(salary); } public void publishSalary(double salary){ System.out.println("Salary of "+empID+" ="+salary); try { PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("G://salary.text", true))); out.println(empID+" "+salary); out.close(); } catch (IOException ex) { Logger.getLogger(Salary.class.getName()).log(Level.SEVERE, null, ex); } loggingForm(); } }

Sign up to vote on this title
UsefulNot useful