Sign in with
Sign up | Sign in
Your question

Java Help!!

Last response: in Applications
Share
October 25, 2011 1:59:58 PM

Hi

I'm writing a program in java to obtain a few queries. I have managed to do most of it but I am struggling to find an avg for the marks in an array that has been inputted by the user. I have 3 classes: student class, person class, and a client class.

this is the code that i have so far:

person class:

  1. public class Person
  2. {
  3.  
  4. private String title;
  5. private String firstname;
  6. private String lastname;
  7. private String dateofbirth;
  8. String n1;
  9. String n2;
  10.  
  11. public Person()
  12. {
  13. title = "No title, ";
  14. firstname = "no first name, ";
  15. lastname = "no last name ";
  16. dateofbirth= "and no date of birth given.";
  17.  
  18. }
  19.  
  20. public Person(String initialTitle, String initialFirstName, String initialSecondName, String initialDateOfBirth)
  21. {
  22. title = initialTitle;
  23. firstname = initialFirstName;
  24. lastname = initialSecondName;
  25. dateofbirth=initialDateOfBirth;
  26. }
  27.  
  28. public void setName(String newTitle, String newFirstName, String newSecondName, String newDateOfBirth)
  29. {
  30. title = newTitle;
  31. firstname = newFirstName;
  32. lastname = newSecondName;
  33. dateofbirth = newDateOfBirth;
  34. }
  35.  
  36. public String getName()
  37. {
  38.  
  39. n1 = (title + " " + firstname + " " + lastname + " " + dateofbirth);
  40. return (n1);
  41. }
  42.  
  43. public String getNameInfo()
  44. {
  45.  
  46. n2 = (firstname + " " + lastname);
  47. return (n2);
  48. }
  49. public void writeOutput()
  50. {
  51. System.out.println("First Name: " + getName());
  52. //System.out.println("Last Name:" + lastname);
  53. }
  54.  
  55. public boolean sameName(Person otherPerson)
  56. {
  57. return(this.n1.equalsIgnoreCase(otherPerson.n1));
  58. }
  59. }



Student class:
  1. public class Student extends Person
  2. {
  3. private long studentNumber;
  4. double overallmark;
  5. double asgmt1, asgmt2;
  6. double prac;
  7. double exams;
  8. double mark1, mark2, mark3;
  9. double total;
  10. double sum;
  11. Scanner keyboard = new Scanner(System.in);
  12. public Student()
  13. {
  14. super();
  15. studentNumber = 0;
  16. }
  17.  
  18. public Student(String initialTitle, String initialFirstName, String initialLastName, long initialStudentNumber, String initialDateOfBirth)
  19. {
  20. super(initialTitle, initialFirstName, initialLastName, initialDateOfBirth);
  21. studentNumber = initialStudentNumber;
  22. }
  23.  
  24. public void reset(String newTitle, String newFirstName,String newLastName, long newStudentNumber, String newDateOfBirth)
  25. {
  26. setName(newTitle, newFirstName, newLastName, newDateOfBirth);
  27. studentNumber = newStudentNumber;
  28. }
  29.  
  30. public long getStudentNumber()
  31. {
  32. return studentNumber;
  33. }
  34.  
  35. public void setStudentNumber(long newStudentNumber)
  36. {
  37. studentNumber = newStudentNumber;
  38. }
  39.  
  40. public void writeOutput()
  41. {
  42. System.out.println("Student: " + getName());
  43. System.out.println("Student Number: " + studentNumber);
  44. }
  45.  
  46. public void InfoOutput()
  47. {
  48. System.out.println("Student: " + getName());
  49. System.out.println("Student Number: " + studentNumber);
  50. System.out.println("Assignment 1: " + asgmt1);
  51. System.out.println("Assignment 2: " + asgmt2);
  52. System.out.println("Practical Work: " + prac);
  53. System.out.println("Final Examinations: " + exams);
  54. System.out.println("Overrall Mark: " + overallmark);
  55. }
  56.  
  57. public boolean equals(Student otherStudent)
  58. {
  59. return(this.sameName(otherStudent) && (this.studentNumber == otherStudent.studentNumber));
  60. }
  61.  
  62. public double asgmtInfo()
  63. {
  64. do
  65. {
  66. System.out.print("Please enter mark for Assignment 1: ");
  67. asgmt1 = keyboard.nextDouble();
  68. System.out.print("Please enter mark for Assignment 2: ");
  69. asgmt2 = keyboard.nextDouble();
  70. if(asgmt1>100 || asgmt2 >100)
  71. {
  72. System.out.println("Please enter a valid number between 0-100 inclusive.");
  73. }
  74.  
  75. }while(asgmt1>100 || asgmt2 >100);
  76. sum = asgmt1+ asgmt2;
  77.  
  78. return sum;
  79. }
  80. public double pracInfo()
  81. {
  82.  
  83. do
  84. {
  85. System.out.print("Please enter mark for Practical work: ");
  86. prac = keyboard.nextDouble();
  87. if(prac>10)
  88. {
  89. System.out.println("Please enter a valid number between 0-10 inclusive.");
  90. }
  91. }while(prac>10);
  92. return prac;
  93. }
  94.  
  95. public double examsInfo()
  96. {
  97.  
  98. do
  99. {
  100. System.out.print("Please enter mark for final examination: ");
  101. exams = keyboard.nextDouble();
  102. if(exams>100)
  103. {
  104. System.out.println("Please enter a valid number between 0-100 inclusive.");
  105. }
  106. }while(exams>100);
  107. return exams;
  108. }
  109.  
  110. public void overallMark()
  111. {
  112. asgmtInfo();
  113. pracInfo();
  114. examsInfo();
  115.  
  116. overallmark = ((((sum)/200)*40)+ ((prac/10)*10) + ((exams/100)*50));
  117. }
  118.  
  119. public void grade()
  120. {
  121.  
  122. if(overallmark > 79)
  123. System.out.println("Final Grade is HD for " + getNameInfo());
  124. else
  125. if(overallmark > 69)
  126. System.out.println("Final Grade is D for " + getNameInfo());
  127. else
  128. if(overallmark > 59)
  129. System.out.println("Final Grade is C for " + getNameInfo());
  130. else
  131. if(overallmark > 49)
  132. System.out.println("Final Grade is P for " + getNameInfo());
  133. else
  134. System.out.println("Final Grade is N for " + getNameInfo());
  135. }
  136.  
  137.  
  138. public double avg()
  139. {
  140.  
  141. total = total + overallmark;
  142.  
  143.  
  144. System.out.println(total);
  145. return total;
  146.  
  147. }
  148.  
  149. public double countGrade()
  150. { int hdcount=0, dcount=0, ccount=0, pcount=0, ncount=0;
  151.  
  152. if(overallmark > 79)
  153. {hdcount +=hdcount;}
  154. else
  155. if(overallmark > 69)
  156. {dcount+=dcount;}
  157. else
  158. if(overallmark > 59)
  159. {ccount+=ccount;}
  160. else
  161. if(overallmark > 49)
  162. {pcount+=pcount;}
  163. else
  164. ncount+=ncount;
  165.  
  166. System.out.println(dcount);
  167. System.out.println(ccount);
  168. return total;
  169.  
  170. }
  171.  
  172.  
  173.  
  174. public void getDetails()
  175. {
  176. long studentNo2;
  177. System.out.print("Please enter student Number: ");
  178. studentNo2=keyboard.nextLong();
  179.  
  180.  
  181. }
  182.  
  183. }


Client class:

  1. import java.util.*;
  2.  
  3. public class InformationDemo {
  4.  
  5. static int nops;
  6. static Scanner kb = new Scanner(System.in);
  7. static Scanner keyboard = new Scanner(System.in);
  8. Student [] pupil = null;
  9. static double [] tot = null;
  10. public static void main(String[] args)
  11. {
  12. String title1;
  13. String firstname1;
  14. String lastname1;
  15. String dateofbirth1;
  16. long studentNo1;
  17. double[] tot;
  18. char[] grade;
  19. nops =0;
  20. char no;
  21.  
  22. System.out.print("Please enter the number of student details to enter into system: ");
  23. nops = keyboard.nextInt();
  24. Student [] pupil = new Student[nops];
  25. grade = new char[nops];
  26. tot = new double[nops];
  27. Student s = new Student();
  28. s.writeOutput();
  29.  
  30. for(int i = 0; i<pupil.length; i++)
  31. {
  32. pupil[i] = new Student();
  33.  
  34. System.out.print("Please enter Title: ");
  35. title1 = kb.nextLine();
  36.  
  37. System.out.print("Please enter First Name (Given Name): ");
  38. firstname1= kb.nextLine();
  39.  
  40. System.out.print("Please Last Name (Family Name/Surname): ");
  41. lastname1 = kb.nextLine();
  42.  
  43. System.out.print("Please enter Date of Birth(DD/MM/YYYY): ");
  44. dateofbirth1=kb.nextLine();
  45.  
  46. System.out.print("Please enter student Number: ");
  47. studentNo1=keyboard.nextLong();
  48.  
  49. pupil[i].setName(title1, firstname1, lastname1, dateofbirth1);
  50. pupil[i].setStudentNumber(studentNo1);
  51. pupil[i].writeOutput();
  52. }
  53. do
  54. {
  55. System.out.println("Please select a number from the menu below to execute the needed operation:");
  56. System.out.println("1 - Quit (exit the program)");
  57. System.out.println("2 - Add all information about a student (except the overall mark and the grade)and determine the student’s grade.");
  58. System.out.println("3 - Output the details (all information) of all students currently held.");
  59. System.out.println("4 - Compute and output the average overall mark for students currently held.");
  60. System.out.println("5 - Display how many students obtained an overall mark equal to or above the average overall mark and how many obtained an overall mark below the average overall mark.");
  61. System.out.println("6 - Display the distribution of grades (i.e., the number of HDs, Ds etc) awarded.");
  62. System.out.println("7 - Provide a student number (ID) and view all details of the student with that number.");
  63. System.out.println("8 - Provide a student’s name (both surname and given name) and view all details of that student.");
  64. System.out.println("9 - Find students with the highest overall mark and the second highest overall mark and display their names and overall marks.");
  65. System.out.println("10 - Display students’ numbers (IDs) in acsending order.");
  66. System.out.println("11 - Display students’ surnames in ascending order.");
  67.  
  68. {
  69. System.out.print("Please input option: ");
  70. no = kb.nextLine().trim().toLowerCase().charAt(0);
  71.  
  72. }
  73. switch(no)
  74. {
  75. case '1':
  76. break;
  77.  
  78. case '2': StudentInfo(pupil);
  79. break;
  80.  
  81. case '3': displayDetails(pupil);
  82. break;
  83.  
  84. case '4': displayAvg(pupil);
  85. break;
  86.  
  87.  
  88. case '5': GradeDis(pupil);
  89. break;
  90.  
  91. case '6': SearchStuID(pupil);
  92. break;
  93.  
  94. default:
  95. System.out.println("Please enter a valid option");
  96. break;
  97.  
  98. } // end switch
  99. } // end do
  100. while(no!='1');
  101.  
  102. }
  103.  
  104.  
  105. public static void StudentInfo(Student[] arr)
  106. {
  107.  
  108. for(int i = 0; i<nops; i++)
  109. {
  110. arr[i].overallMark();
  111. arr[i].grade();
  112.  
  113. System.out.println();
  114. }
  115. }
  116.  
  117. public static void displayDetails(Student[] arr)
  118. {
  119. for (int i=0; i<nops; i++)
  120. {
  121. arr[i].InfoOutput();
  122. System.out.println();
  123. }
  124. }
  125.  
  126. public static void displayAvg(Student[] arr)
  127. {
  128. {
  129. for (int i=0; i<nops; i++)
  130. {
  131. for(int j=0;j<arr.length;j++)
  132. arr[i].avg();
  133. avg = arr[i].avg()+
  134. nops++;
  135. }
  136. for (int j=0; j<arr.length;j++)
  137. {
  138.  
  139. }
  140. System.out.println();
  141. }
  142.  
  143. }
  144.  
  145. public static void GradeDis(Student[] arr)
  146. {
  147. for (int i=0; i<nops; i++)
  148. {
  149. arr[i].countGrade();
  150. System.out.println();
  151. }
  152. }
  153.  
  154. public static void searchStuID(Student[] arr)
  155. {
  156. for (int i=0; i<nops; i++)
  157. {
  158. long studentNo2;
  159. System.out.print("Please enter student Number: ");
  160. studentNo2=keyboard.nextLong();
  161. arr[i].getDetails();
  162. System.out.println();
  163. }
  164. }
  165. }


Thank you so much, really appreciate it.

More about : java

October 26, 2011 1:26:31 AM

I've managed to sort it out, but thanks all...
m
0
l
!