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:
[cpp]public class Person
{
private String title;
private String firstname;
private String lastname;
private String dateofbirth;
String n1;
String n2;
public Person()
{
title = "No title, ";
firstname = "no first name, ";
lastname = "no last name ";
dateofbirth= "and no date of birth given.";
}
public Person(String initialTitle, String initialFirstName, String initialSecondName, String initialDateOfBirth)
{
title = initialTitle;
firstname = initialFirstName;
lastname = initialSecondName;
dateofbirth=initialDateOfBirth;
}
public void setName(String newTitle, String newFirstName, String newSecondName, String newDateOfBirth)
{
title = newTitle;
firstname = newFirstName;
lastname = newSecondName;
dateofbirth = newDateOfBirth;
}
public String getName()
{
n1 = (title + " " + firstname + " " + lastname + " " + dateofbirth);
return (n1);
}
public String getNameInfo()
{
n2 = (firstname + " " + lastname);
return (n2);
}
public void writeOutput()
{
System.out.println("First Name: " + getName());
//System.out.println("Last Name:" + lastname);
}
public boolean sameName(Person otherPerson)
{
return(this.n1.equalsIgnoreCase(otherPerson.n1));
}
}[/cpp]
Student class:
[cpp]
public class Student extends Person
{
private long studentNumber;
double overallmark;
double asgmt1, asgmt2;
double prac;
double exams;
double mark1, mark2, mark3;
double total;
double sum;
Scanner keyboard = new Scanner(System.in);
public Student()
{
super();
studentNumber = 0;
}
public Student(String initialTitle, String initialFirstName, String initialLastName, long initialStudentNumber, String initialDateOfBirth)
{
super(initialTitle, initialFirstName, initialLastName, initialDateOfBirth);
studentNumber = initialStudentNumber;
}
public void reset(String newTitle, String newFirstName,String newLastName, long newStudentNumber, String newDateOfBirth)
{
setName(newTitle, newFirstName, newLastName, newDateOfBirth);
studentNumber = newStudentNumber;
}
public long getStudentNumber()
{
return studentNumber;
}
public void setStudentNumber(long newStudentNumber)
{
studentNumber = newStudentNumber;
}
public void writeOutput()
{
System.out.println("Student: " + getName());
System.out.println("Student Number: " + studentNumber);
}
public void InfoOutput()
{
System.out.println("Student: " + getName());
System.out.println("Student Number: " + studentNumber);
System.out.println("Assignment 1: " + asgmt1);
System.out.println("Assignment 2: " + asgmt2);
System.out.println("Practical Work: " + prac);
System.out.println("Final Examinations: " + exams);
System.out.println("Overrall Mark: " + overallmark);
}
public boolean equals(Student otherStudent)
{
return(this.sameName(otherStudent) && (this.studentNumber == otherStudent.studentNumber));
}
public double asgmtInfo()
{
do
{
System.out.print("Please enter mark for Assignment 1: ");
asgmt1 = keyboard.nextDouble();
System.out.print("Please enter mark for Assignment 2: ");
asgmt2 = keyboard.nextDouble();
if(asgmt1>100 || asgmt2 >100)
{
System.out.println("Please enter a valid number between 0-100 inclusive.");
}
}while(asgmt1>100 || asgmt2 >100);
sum = asgmt1+ asgmt2;
return sum;
}
public double pracInfo()
{
do
{
System.out.print("Please enter mark for Practical work: ");
prac = keyboard.nextDouble();
if(prac>10)
{
System.out.println("Please enter a valid number between 0-10 inclusive.");
}
}while(prac>10);
return prac;
}
public double examsInfo()
{
do
{
System.out.print("Please enter mark for final examination: ");
exams = keyboard.nextDouble();
if(exams>100)
{
System.out.println("Please enter a valid number between 0-100 inclusive.");
}
}while(exams>100);
return exams;
}
public void overallMark()
{
asgmtInfo();
pracInfo();
examsInfo();
overallmark = ((((sum)/200)*40)+ ((prac/10)*10) + ((exams/100)*50));
}
public void grade()
{
if(overallmark > 79)
System.out.println("Final Grade is HD for " + getNameInfo());
else
if(overallmark > 69)
System.out.println("Final Grade is D for " + getNameInfo());
else
if(overallmark > 59)
System.out.println("Final Grade is C for " + getNameInfo());
else
if(overallmark > 49)
System.out.println("Final Grade is P for " + getNameInfo());
else
System.out.println("Final Grade is N for " + getNameInfo());
}
public double avg()
{
total = total + overallmark;
System.out.println(total);
return total;
}
public double countGrade()
{ int hdcount=0, dcount=0, ccount=0, pcount=0, ncount=0;
if(overallmark > 79)
{hdcount +=hdcount;}
else
if(overallmark > 69)
{dcount+=dcount;}
else
if(overallmark > 59)
{ccount+=ccount;}
else
if(overallmark > 49)
{pcount+=pcount;}
else
ncount+=ncount;
System.out.println(dcount);
System.out.println(ccount);
return total;
}
public void getDetails()
{
long studentNo2;
System.out.print("Please enter student Number: ");
studentNo2=keyboard.nextLong();
}
}[/cpp]
Client class:
[cpp]
import java.util.*;
public class InformationDemo {
static int nops;
static Scanner kb = new Scanner(System.in);
static Scanner keyboard = new Scanner(System.in);
Student [] pupil = null;
static double [] tot = null;
public static void main(String[] args)
{
String title1;
String firstname1;
String lastname1;
String dateofbirth1;
long studentNo1;
double[] tot;
char[] grade;
nops =0;
char no;
System.out.print("Please enter the number of student details to enter into system: ");
nops = keyboard.nextInt();
Student [] pupil = new Student[nops];
grade = new char[nops];
tot = new double[nops];
Student s = new Student();
s.writeOutput();
for(int i = 0; i<pupil.length; i++)
{
pupil = new Student();
System.out.print("Please enter Title: ");
title1 = kb.nextLine();
System.out.print("Please enter First Name (Given Name): ");
firstname1= kb.nextLine();
System.out.print("Please Last Name (Family Name/Surname): ");
lastname1 = kb.nextLine();
System.out.print("Please enter Date of Birth(DD/MM/YYYY): ");
dateofbirth1=kb.nextLine();
System.out.print("Please enter student Number: ");
studentNo1=keyboard.nextLong();
pupil.setName(title1, firstname1, lastname1, dateofbirth1);
pupil.setStudentNumber(studentNo1);
pupil.writeOutput();
}
do
{
System.out.println("Please select a number from the menu below to execute the needed operation:");
System.out.println("1 - Quit (exit the program)");
System.out.println("2 - Add all information about a student (except the overall mark and the grade)and determine the student’s grade.");
System.out.println("3 - Output the details (all information) of all students currently held.");
System.out.println("4 - Compute and output the average overall mark for students currently held.");
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.");
System.out.println("6 - Display the distribution of grades (i.e., the number of HDs, Ds etc) awarded.");
System.out.println("7 - Provide a student number (ID) and view all details of the student with that number.");
System.out.println("8 - Provide a student’s name (both surname and given name) and view all details of that student.");
System.out.println("9 - Find students with the highest overall mark and the second highest overall mark and display their names and overall marks.");
System.out.println("10 - Display students’ numbers (IDs) in acsending order.");
System.out.println("11 - Display students’ surnames in ascending order.");
{
System.out.print("Please input option: ");
no = kb.nextLine().trim().toLowerCase().charAt(0);
}
switch(no)
{
case '1':
break;
case '2': StudentInfo(pupil);
break;
case '3': displayDetails(pupil);
break;
case '4': displayAvg(pupil);
break;
case '5': GradeDis(pupil);
break;
case '6': SearchStuID(pupil);
break;
default:
System.out.println("Please enter a valid option");
break;
} // end switch
} // end do
while(no!='1');
}
public static void StudentInfo(Student[] arr)
{
for(int i = 0; i<nops; i++)
{
arr.overallMark();
arr.grade();
System.out.println();
}
}
public static void displayDetails(Student[] arr)
{
for (int i=0; i<nops; i++)
{
arr.InfoOutput();
System.out.println();
}
}
public static void displayAvg(Student[] arr)
{
{
for (int i=0; i<nops; i++)
{
for(int j=0;j<arr.length;j++)
arr.avg();
avg = arr.avg()+
nops++;
}
for (int j=0; j<arr.length;j++)
{
}
System.out.println();
}
}
public static void GradeDis(Student[] arr)
{
for (int i=0; i<nops; i++)
{
arr.countGrade();
System.out.println();
}
}
public static void searchStuID(Student[] arr)
{
for (int i=0; i<nops; i++)
{
long studentNo2;
System.out.print("Please enter student Number: ");
studentNo2=keyboard.nextLong();
arr.getDetails();
System.out.println();
}
}
}[/cpp]
Thank you so much, really appreciate it.
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:
[cpp]public class Person
{
private String title;
private String firstname;
private String lastname;
private String dateofbirth;
String n1;
String n2;
public Person()
{
title = "No title, ";
firstname = "no first name, ";
lastname = "no last name ";
dateofbirth= "and no date of birth given.";
}
public Person(String initialTitle, String initialFirstName, String initialSecondName, String initialDateOfBirth)
{
title = initialTitle;
firstname = initialFirstName;
lastname = initialSecondName;
dateofbirth=initialDateOfBirth;
}
public void setName(String newTitle, String newFirstName, String newSecondName, String newDateOfBirth)
{
title = newTitle;
firstname = newFirstName;
lastname = newSecondName;
dateofbirth = newDateOfBirth;
}
public String getName()
{
n1 = (title + " " + firstname + " " + lastname + " " + dateofbirth);
return (n1);
}
public String getNameInfo()
{
n2 = (firstname + " " + lastname);
return (n2);
}
public void writeOutput()
{
System.out.println("First Name: " + getName());
//System.out.println("Last Name:" + lastname);
}
public boolean sameName(Person otherPerson)
{
return(this.n1.equalsIgnoreCase(otherPerson.n1));
}
}[/cpp]
Student class:
[cpp]
public class Student extends Person
{
private long studentNumber;
double overallmark;
double asgmt1, asgmt2;
double prac;
double exams;
double mark1, mark2, mark3;
double total;
double sum;
Scanner keyboard = new Scanner(System.in);
public Student()
{
super();
studentNumber = 0;
}
public Student(String initialTitle, String initialFirstName, String initialLastName, long initialStudentNumber, String initialDateOfBirth)
{
super(initialTitle, initialFirstName, initialLastName, initialDateOfBirth);
studentNumber = initialStudentNumber;
}
public void reset(String newTitle, String newFirstName,String newLastName, long newStudentNumber, String newDateOfBirth)
{
setName(newTitle, newFirstName, newLastName, newDateOfBirth);
studentNumber = newStudentNumber;
}
public long getStudentNumber()
{
return studentNumber;
}
public void setStudentNumber(long newStudentNumber)
{
studentNumber = newStudentNumber;
}
public void writeOutput()
{
System.out.println("Student: " + getName());
System.out.println("Student Number: " + studentNumber);
}
public void InfoOutput()
{
System.out.println("Student: " + getName());
System.out.println("Student Number: " + studentNumber);
System.out.println("Assignment 1: " + asgmt1);
System.out.println("Assignment 2: " + asgmt2);
System.out.println("Practical Work: " + prac);
System.out.println("Final Examinations: " + exams);
System.out.println("Overrall Mark: " + overallmark);
}
public boolean equals(Student otherStudent)
{
return(this.sameName(otherStudent) && (this.studentNumber == otherStudent.studentNumber));
}
public double asgmtInfo()
{
do
{
System.out.print("Please enter mark for Assignment 1: ");
asgmt1 = keyboard.nextDouble();
System.out.print("Please enter mark for Assignment 2: ");
asgmt2 = keyboard.nextDouble();
if(asgmt1>100 || asgmt2 >100)
{
System.out.println("Please enter a valid number between 0-100 inclusive.");
}
}while(asgmt1>100 || asgmt2 >100);
sum = asgmt1+ asgmt2;
return sum;
}
public double pracInfo()
{
do
{
System.out.print("Please enter mark for Practical work: ");
prac = keyboard.nextDouble();
if(prac>10)
{
System.out.println("Please enter a valid number between 0-10 inclusive.");
}
}while(prac>10);
return prac;
}
public double examsInfo()
{
do
{
System.out.print("Please enter mark for final examination: ");
exams = keyboard.nextDouble();
if(exams>100)
{
System.out.println("Please enter a valid number between 0-100 inclusive.");
}
}while(exams>100);
return exams;
}
public void overallMark()
{
asgmtInfo();
pracInfo();
examsInfo();
overallmark = ((((sum)/200)*40)+ ((prac/10)*10) + ((exams/100)*50));
}
public void grade()
{
if(overallmark > 79)
System.out.println("Final Grade is HD for " + getNameInfo());
else
if(overallmark > 69)
System.out.println("Final Grade is D for " + getNameInfo());
else
if(overallmark > 59)
System.out.println("Final Grade is C for " + getNameInfo());
else
if(overallmark > 49)
System.out.println("Final Grade is P for " + getNameInfo());
else
System.out.println("Final Grade is N for " + getNameInfo());
}
public double avg()
{
total = total + overallmark;
System.out.println(total);
return total;
}
public double countGrade()
{ int hdcount=0, dcount=0, ccount=0, pcount=0, ncount=0;
if(overallmark > 79)
{hdcount +=hdcount;}
else
if(overallmark > 69)
{dcount+=dcount;}
else
if(overallmark > 59)
{ccount+=ccount;}
else
if(overallmark > 49)
{pcount+=pcount;}
else
ncount+=ncount;
System.out.println(dcount);
System.out.println(ccount);
return total;
}
public void getDetails()
{
long studentNo2;
System.out.print("Please enter student Number: ");
studentNo2=keyboard.nextLong();
}
}[/cpp]
Client class:
[cpp]
import java.util.*;
public class InformationDemo {
static int nops;
static Scanner kb = new Scanner(System.in);
static Scanner keyboard = new Scanner(System.in);
Student [] pupil = null;
static double [] tot = null;
public static void main(String[] args)
{
String title1;
String firstname1;
String lastname1;
String dateofbirth1;
long studentNo1;
double[] tot;
char[] grade;
nops =0;
char no;
System.out.print("Please enter the number of student details to enter into system: ");
nops = keyboard.nextInt();
Student [] pupil = new Student[nops];
grade = new char[nops];
tot = new double[nops];
Student s = new Student();
s.writeOutput();
for(int i = 0; i<pupil.length; i++)
{
pupil = new Student();
System.out.print("Please enter Title: ");
title1 = kb.nextLine();
System.out.print("Please enter First Name (Given Name): ");
firstname1= kb.nextLine();
System.out.print("Please Last Name (Family Name/Surname): ");
lastname1 = kb.nextLine();
System.out.print("Please enter Date of Birth(DD/MM/YYYY): ");
dateofbirth1=kb.nextLine();
System.out.print("Please enter student Number: ");
studentNo1=keyboard.nextLong();
pupil.setName(title1, firstname1, lastname1, dateofbirth1);
pupil.setStudentNumber(studentNo1);
pupil.writeOutput();
}
do
{
System.out.println("Please select a number from the menu below to execute the needed operation:");
System.out.println("1 - Quit (exit the program)");
System.out.println("2 - Add all information about a student (except the overall mark and the grade)and determine the student’s grade.");
System.out.println("3 - Output the details (all information) of all students currently held.");
System.out.println("4 - Compute and output the average overall mark for students currently held.");
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.");
System.out.println("6 - Display the distribution of grades (i.e., the number of HDs, Ds etc) awarded.");
System.out.println("7 - Provide a student number (ID) and view all details of the student with that number.");
System.out.println("8 - Provide a student’s name (both surname and given name) and view all details of that student.");
System.out.println("9 - Find students with the highest overall mark and the second highest overall mark and display their names and overall marks.");
System.out.println("10 - Display students’ numbers (IDs) in acsending order.");
System.out.println("11 - Display students’ surnames in ascending order.");
{
System.out.print("Please input option: ");
no = kb.nextLine().trim().toLowerCase().charAt(0);
}
switch(no)
{
case '1':
break;
case '2': StudentInfo(pupil);
break;
case '3': displayDetails(pupil);
break;
case '4': displayAvg(pupil);
break;
case '5': GradeDis(pupil);
break;
case '6': SearchStuID(pupil);
break;
default:
System.out.println("Please enter a valid option");
break;
} // end switch
} // end do
while(no!='1');
}
public static void StudentInfo(Student[] arr)
{
for(int i = 0; i<nops; i++)
{
arr.overallMark();
arr.grade();
System.out.println();
}
}
public static void displayDetails(Student[] arr)
{
for (int i=0; i<nops; i++)
{
arr.InfoOutput();
System.out.println();
}
}
public static void displayAvg(Student[] arr)
{
{
for (int i=0; i<nops; i++)
{
for(int j=0;j<arr.length;j++)
arr.avg();
avg = arr.avg()+
nops++;
}
for (int j=0; j<arr.length;j++)
{
}
System.out.println();
}
}
public static void GradeDis(Student[] arr)
{
for (int i=0; i<nops; i++)
{
arr.countGrade();
System.out.println();
}
}
public static void searchStuID(Student[] arr)
{
for (int i=0; i<nops; i++)
{
long studentNo2;
System.out.print("Please enter student Number: ");
studentNo2=keyboard.nextLong();
arr.getDetails();
System.out.println();
}
}
}[/cpp]
Thank you so much, really appreciate it.