diff --git a/GPAcalc/.classpath b/GPAcalc/.classpath new file mode 100644 index 0000000..12fc71b --- /dev/null +++ b/GPAcalc/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/GPAcalc/.gitignore b/GPAcalc/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/GPAcalc/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/GPAcalc/.project b/GPAcalc/.project new file mode 100644 index 0000000..d915dd7 --- /dev/null +++ b/GPAcalc/.project @@ -0,0 +1,17 @@ + + + GPAcalc + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/GPAcalc/.settings/org.eclipse.jdt.core.prefs b/GPAcalc/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..bb35fa0 --- /dev/null +++ b/GPAcalc/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/GPAcalc/courses.txt b/GPAcalc/courses.txt new file mode 100644 index 0000000..21aa5e6 --- /dev/null +++ b/GPAcalc/courses.txt @@ -0,0 +1,4 @@ +B;CS 106 Introduction to C +A;CS 466 Introduction to Java +A;CS 206 Adnanced database +C;CS 224 Cpmputer Security diff --git a/GPAcalc/src/gpa/Calculator.java b/GPAcalc/src/gpa/Calculator.java new file mode 100644 index 0000000..548a327 --- /dev/null +++ b/GPAcalc/src/gpa/Calculator.java @@ -0,0 +1,171 @@ + +package gpa; + +/** + * @author khalid + */ +import java.util.*; +import java.io.*; + +/** -------------------------------------------------- data member ------------------------------------------- */ + +public class Calculator { + private ArrayList courses; + private Course c; // to save what we get from the array list + private double GpaAvg; + private Scanner in= new Scanner(System.in);// read from keyboard + private Scanner input; // read from file + private PrintWriter output ; // print to file + private File f; // creay a file + +/** ----------------------------------------- constructor function ------------------------------------------ */ +/** 1. To allocate memory the array list + 2. To save the infromation that gets from the file in the array list + 3. To write what the array list contains back to the file */ + + public Calculator(){ + courses= new ArrayList(); + + try{ //Read from file and save info in the Arraylist + f = new File("courses.txt"); + if( f.exists()) // if file is exist + { + + if( f.length() == 0) // if file is empty to prevent throw an exption + { + System.out.println("Warning: The file is empty"); + } + else // if the file not empty at least has one line + { + input = new Scanner(new FileReader("courses.txt")); + while(input.hasNextLine()){ + String line = input.nextLine(); + String parts[] = line.split(";"); + + courses.add( new Course (parts[0],parts[1])); + }//end while + input.close(); + }// end else + + }// end if existe + else System.out.println("Error: the courses.txt file is not exist.\nThe program will create courses.txt file"); + + } catch (FileNotFoundException e) {e.printStackTrace();} + + try { // write info from arraylist to the file + output = new PrintWriter(f); + for (int i=0; i< courses.size(); i++){ + output.println((Course) courses.get(i)); + } + output.close(); + + }catch (IOException ex){ex.printStackTrace();} + } + + + /* function to let the user enter the course and the GPA + public void interCours(){ + String course; + String degree; + + System.out.print("Enter course name: "); + course=in.next(); + do{ + System.out.print("Enter GPA(A,B,C,F): "); + degree= in.nextLine(); + if (degree.equals("A") ||degree.equals("A") ||degree.equals("b")||degree.equals("B")||degree.equals("C")||degree.equals("c")||degree.equals("f")||degree.equals("F")) break; + String junck=in.nextLine(); + }while (true); + courses.add(new Course(degree,course)); + } */ + + /** ------------------------ display Courses And GPA ------------------------- */ + + public void displayCourses (){ + String GPA,cname; + if(courses.size()>0){ + System.out.println("Course name\t\t\t\t GPA"); + System.out.println("-----------\t\t\t\t ---"); + for(int i=0; i0){ + GpaAvg=(double)GpaTotal /(double)courses.size(); + System.out.printf("\n** The Average GPA is %.2f **\n ",GpaAvg); + }else{ + GpaAvg=0.0; // to prevent NAN result + System.out.printf("** Avarage of the GPA is %.2f**\n ",GpaAvg); + } + } + + /** -------------------- Main Function creat new object and call dispaly and calulate function --------------- */ + + public static void main(String[] args ){ + + try { + Calculator objCourse = new Calculator(); + objCourse.displayCourses(); + objCourse.caluclate(); + } catch (Exception ex) { + System.out.println("Error: "+ex); + ex.printStackTrace(); + System.exit(0); + } + + } +} + diff --git a/GPAcalc/src/gpa/Course.java b/GPAcalc/src/gpa/Course.java new file mode 100644 index 0000000..73749d5 --- /dev/null +++ b/GPAcalc/src/gpa/Course.java @@ -0,0 +1,39 @@ +package gpa; + + +/** + * @author khalid + * + * Exam Program by khalid + * User: khalid + * Date: 2/14/13 + */ +public class Course { + + private String courseName; + private String GPA; + + Course(String GPA, String courseName ){ + this.courseName = courseName; + this.GPA = GPA; + + } + + public String getCourse() { + return courseName; + } + public String getGPA() { + return GPA; + } + + + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append(GPA+";"); + sb.append(courseName); + return sb.toString(); + + } + + +}