Programming language - Java In this assignment, you will create your own databas
ID: 3695980 • Letter: P
Question
Programming language - Java
In this assignment, you will create your own database system, one that helps us answer questions about students and their GPAs.
Our “database” will be a text file. The text file has the following format:
Name Code GPA
The name field stores the name of the student. The GPA field stores the student’s GPA. The Code field stores demographic data about the student in a very compact way. The code consists of 5 characters, in this order:
Gender, (M or F)
Race / Ethnicity (W for white, B for Black / African-American, H for Hispanic, A for Asian, O for other
Location (R for residential, C for commuter)
College (A for Arts and Sciences, B for Business, E for Education, N for Nursing)
Year (1 for freshman, 2 for sophomore, 3 for junior, 4 for senior, G for grad)
Here’s an example of a data file formatted in this way:
Joe MWCA3 3.42
Fred MARB4 2.75
Kris FWCB3 3.12
Sonia FHCA2 3.51
Tom MWRE3 3.91
Frank MHRA3 3.55
Ally FBRA4 3.14
Ed MWCN1 2.79
Nina FHRA3 3.74
Jasmine FBCB4 3.28
Alex MWRAG 3.12
Robert MBCE2 3.07
Simone FWRE3 3.19
Ray MWCA2 2.74
Steve MWCA3 2.99
One use of this database system is to link GPA with demographic information. The user will be able to ask questions such as “What is the average GPA for all male business majors” and “What is the average GPA for all female African-American or Hispanic Commuter students.” Even with this rather limited data set, we can ask very specific questions.
We will write a program that will read this data and then enable users to compute average GPA values for specific subgroups of people. We won’t ask quite as specific kinds of questions as we could, for we will limit ourselves to asking questions that identify people that meet only one condition per category. In other words, we won’t ask if someone is either white or Hispanic, or either in Nursing or Arts and Sciences. Instead, we will ask whether they meet on of the conditions in each category. This will simply the logic considerably.
Your program will read the file of student data. It will then have the user specify which records he is interested in by asking him to answer a series of questions. Specifically, the user will be asked to specify the gender, ethnicity, residential status, college, and year. If the user doesn’t care about the answer to a particular question (for example, he wants all students, regardless of their gender), then he will enter an asterisk. After the user answers these questions, the program will show a list of all students who match these critiera. It will then show the average gpa for those students. The program will then ask the user if he wants to identify a different set of students. If he does, the program will repeat the process. The program ends when the user indicates he wants to quit.
Here is an example of the output your program should produce:
Welcome to GPA Calculator
This program computes the GPA of groups of students
based on data about individual student demographics
and GPAs read from a text file. It implements the kind
of functionality you might find from a modern database.
Enter the name of the data file: c: empgpa.txt
This is the data that was read from the file:
Joe MWCA3 3.42
Fred MARB4 2.75
Kris FWCB3 3.12
Sonia FHCA2 3.51
Tom MWRE3 3.91
Frank MHRA3 3.55
Ally FBRA4 3.14
Ed MWCN1 2.79
Nina FHRA3 3.74
Jasmine FBCB4 3.28
Alex MWRAG 3.12
Robert MBCE2 3.07
Simone FWRE3 3.19
Ray MWCA2 2.74
Steve MWCA3 2.99
Enter demographic data for which to find average GPA:
Gender
M for Male, F for Female,
* for either: M
Race/Ethnicity
W for White, B for African/American,
H for Hispanic, A for Asian,
O for other, * for any: W
Location
R for residential, C for commuter
* for either: *
College
A for Arts and Sciences, B for Business,
E for Education, N for Nursing,
* for any: A
Year
1 for Freshman, 2 for Sophomore,
3 for Junior, 4 for Senior,
G for Grad, * for any: *
The following students meet those criteria:
Joe 3.42
Alex 3.12
Ray 2.74
Steve 2.99
The average GPA for that demographic is 3.07.
Would you like to compute another (Y or N)? Y
Enter demographic data for which to find average GPA:
Gender
M for Male, F for Female,
* for either: F
Race/Ethnicity
W for White, B for African/American,
H for Hispanic, A for Asian,
O for other, * for any: B
Location
R for residential, C for commuter
* for either: *
College
A for Arts and Sciences, B for Business,
E for Education, N for Nursing,
* for any: *
Year
1 for Freshman, 2 for Sophomore,
3 for Junior, 4 for Senior,
G for Grad, * for any: *
The following students meet those criteria:
Ally 3.14
Jasmine 3.28
The average GPA for that demographic is 3.21.
Would you like to compute another (Y or N)? Y
Enter demographic data for which to find average GPA:
Gender
M for Male, F for Female,
* for either: *
Race/Ethnicity
W for White, B for African/American,
H for Hispanic, A for Asian,
O for other, * for any: *
Location
R for residential, C for commuter
* for either: R
College
A for Arts and Sciences, B for Business,
E for Education, N for Nursing,
* for any: B
Year
1 for Freshman, 2 for Sophomore,
3 for Junior, 4 for Senior,
G for Grad, * for any: *
The following students meet those criteria:
Fred 2.75
The average GPA for that demographic is 2.75.
Would you like to compute another (Y or N)? Y
Enter demographic data for which to find average GPA:
Gender
M for Male, F for Female,
* for either: *
Race/Ethnicity
W for White, B for African/American,
H for Hispanic, A for Asian,
O for other, * for any: *
Location
R for residential, C for commuter
* for either: *
College
A for Arts and Sciences, B for Business,
E for Education, N for Nursing,
* for any: *
Year
1 for Freshman, 2 for Sophomore,
3 for Junior, 4 for Senior,
G for Grad, * for any: G
The following students meet those criteria:
Alex 3.12
The average GPA for that demographic is 3.12.
Would you like to compute another (Y or N)? N
Your program will be graded as follows:
Your program prints an introductory greeting for the user. 1 point
Your program asks the user for the file to open and correctly opens it. 1 point
Your program reads the records in the file into parallel arrays. 2 points
Your program correctly prints out the student data to the screen 2 points
Your program repeatedly asks the user to specify selection criteria until he indicates he wants to quit. 1 point
Your program correctly selects based on gender. 1 point
Your program correctly selects based on ethnicity. 1 point
Your program correctly selects based on residential status. 1 point
Your program correctly selects based on college. 1 point
Your program correctly selects based on year. 1 point
Your program lists selected students and their gpa to the screen 2 points
Your program correctly computes the average gpa. 2 points
Your program reports 0 as the average gpa if no students match the selected criteria (instead of trying to divide by 0). 1 point
Your program makes use of multiple functions (for example, to print the intro, to print the records, and to select the matching student records). 2 points
Your code is well-documented, properly indented, uses self-explanatory variable names, and is easy-to-follow. 1 point
Explanation / Answer
Following code works fine except the actual calculation of the average. It will take more time implement. Go through the code you will find where to write code for actual calculation.
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class Database {
static String gender, race, location, college, year; //different variable declarations
public static void main(String[] args) throws Exception{ //main method
String choice = "y"; //Initialize choice to y
ArrayList list = new ArrayList(); //create object of ArrayList to store data from file
System.out.println("Enter Path : "); //ask the user to enter file path
Scanner scan = new Scanner(System.in); //create object of scanner class
String path = scan.next();
BufferedReader br=new BufferedReader( new FileReader(path)); //read file using Buffered Reader and File Reader
String currentLine; //point string each line
System.out.println("This is the data that was read from the file : "); //display the data from file
while((currentLine = br.readLine())!=null) {
System.out.println(currentLine);
String words[] = currentLine.split(" ");
for(int i=0;i<words.length;i++) {
list.add(words[i]);
}
}
System.out.println("Enter demographic data for which to find average GPA: ");
while(!choice.equalsIgnoreCase("n") ) {
int count=0;
System.out.println("Gender : (M for Male, F for Female) : ");
gender = scan.next();
System.out.println("Race/Ethnicity: (W for White, B for African/American, H for Hispanic, A for Asian, O for other ) : ");
race = scan.next();
System.out.println("Location: (R for residential, C for commuter) :");
location = scan.next();
System.out.println("College: (A for Arts and Sciences, B for Business, E for Education, N for Nursing) : ");
college = scan.next();
System.out.println("Year: (for Freshman, 2 for Sophomore, for Junior, 4 for Senior, G for Grad ) :");
year = scan.next();
System.out.println("Following students met that criteia : ");
for(int i=0; i<list.size(); i++) {
//Retrieve the students who satisfies the above criteria
}
System.out.println("Would you like to compute another (Y or N)? ");
choice = scan.next(); //alter the value of choice
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.