Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I rewrite this code, so it does not include clone(), private static variables, final variable, and private static methods? It runs perfect but

How can I rewrite this code, so it does not include "clone(), private static variables, final variable, and private static methods?"

It runs perfect but I need it rewritten

The code is below

Here is my code so far:

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class StudentGrades { private static int arraySize = 50; private static String []studentNames = new String[arraySize]; private static int [] studentIDs = new int[arraySize]; private static int [][] assignmentMarks = new int[arraySize][4]; private static int [][] labMarks = new int[arraySize][11]; private static int [][] examMarks = new int[arraySize][2]; private static double [][] weightedMarks = new double[arraySize][8]; private static int numStudents = 0; private static void reallocateArrays() { // double array size variable arraySize *= 2; // Reallocate studentNames // copy array studentNames to tempNames array String[] tempNames = studentNames.clone(); // reallocate the array studentNames = new String[arraySize]; // copy countries from tempNames array System.arraycopy(tempNames, 0, studentNames, 0, tempNames.length); // Reallocate assignmentMarks int[][] tempMarks= assignmentMarks.clone(); assignmentMarks = new int[arraySize][4]; System.arraycopy(tempMarks, 0, assignmentMarks, 0, tempMarks.length); // Reallocate labMarks tempMarks = labMarks.clone(); labMarks = new int[arraySize][11]; System.arraycopy(tempMarks, 0, labMarks, 0, tempMarks.length); // Reallocate examMarks tempMarks= examMarks.clone(); examMarks = new int[arraySize][2]; System.arraycopy(tempMarks, 0, examMarks, 0, tempMarks.length); // Reallocate studentIDs int [] tempIDs= studentIDs.clone(); studentIDs = new int[arraySize]; System.arraycopy(tempIDs, 0, studentIDs, 0, tempIDs.length); } public static void calculateWeightedMarks() { calculateWeightedLabMarks(); calculateWeightedAssignmentMarks(); calculateWeightedExamMarks(); } private static void calculateWeightedLabMarks() { final double percent = 0.10; int sum, min; // for each student for(int i = 0 ;i < numStudents; i++) { sum = 0; min = -1; // for each assignment for(int j = 0; j < 11; j++) { sum+=labMarks[i][j]; if(min < 0 || min > labMarks[i][j]) { min = labMarks[i][j]; } } // 10 best out of 11. sum all 11 marks and subtract minimum mark sum -= min; // calculate weighted mark weightedMarks[i][0] = sum*percent; weightedMarks[i][7] = 0 ; // add to final weightedMarks[i][7] += weightedMarks[i][0]; } } private static void calculateWeightedAssignmentMarks() { final double percent = 0.075; // assignment marks starts at index 1 in weightedMarks int offset = 1; // for each student for(int i = 0 ;i < numStudents; i++) { // for each assignment for(int j = 0; j < 4; j++) { // calculate weighted mark weightedMarks[i][offset+j] = assignmentMarks[i][j]*percent; // add to final weightedMarks[i][7] += weightedMarks[i][offset+j]; } } } private static void calculateWeightedExamMarks() { final double percent_mid = 0.5, percent_final = 0.5; // assignment marks starts at index 1 in weightedMarks int offset = 5; // for each student for(int i = 0 ;i < numStudents; i++) { // calculate weighted mark for mid Term weightedMarks[i][offset] = examMarks[i][0]*percent_mid; // calculate weighted mark for mid Term weightedMarks[i][offset+1] = examMarks[i][1]*percent_final; // add to final weightedMarks[i][7] += weightedMarks[i][offset] + weightedMarks[i][offset+1]; } } public static void displaySummary() { double sum = 0.0; System.out.print(" Student # \tLabs As1 As2 As3 As4 Mid Final Mark"); for(int i = 0; i < numStudents;i++) { System.out.printf(" %-15s %5d\t",studentNames[i], studentIDs[i]); // display weighted marks for(int j = 0 ; j < 8; j++) { System.out.printf("%4.1f\t",weightedMarks[i][j]); sum += weightedMarks[i][j]; } } } private static int getStudentIndex(int sID) { for(int i = 0; i < numStudents; i++) { if(studentIDs[i]== sID) { return i; } } return -1; } public static void main(String[] args)throws FileNotFoundException { Scanner in = new Scanner(new File("A1.txt")); while(in.hasNext()) { // read name studentNames[numStudents] = in.next(); // read ID studentIDs[numStudents] = in.nextInt(); // read lab marks for(int i = 0; i < 11; i++) { labMarks[numStudents][i] = in.nextInt(); } // read assignment marks for(int i = 0; i < 4; i++) { assignmentMarks[numStudents][i] = in.nextInt(); } // read exam marks for(int i = 0; i < 2; i++) { examMarks[numStudents][i] = in.nextInt(); } // increment student count numStudents++; // if arrays are full if(numStudents == arraySize) { reallocateArrays(); } } // process marks calculateWeightedMarks(); // print summary displaySummary(); // display menu String option, change; in = new Scanner(System.in); int studentID, markID, newMark, idx; do { System.out.println(" Options:"); System.out.println("A. Change a student grade"); System.out.println("B. Quit"); option = in.nextLine(); if(option.equals("A")) { System.out.println("Enter the student number:"); studentID = Integer.parseInt(in.nextLine()); System.out.println("Select:"); System.out.println("A. Lab"); System.out.println("B. Assignment"); System.out.println("C. Exam"); change = in.nextLine(); if(change.equals("A")) { System.out.println("Enter Assignment number followed by new mark: "); markID = in.nextInt(); newMark = in.nextInt(); if((idx=getStudentIndex(studentID))>0) { assignmentMarks[idx][markID-1] = newMark; calculateWeightedMarks(); displaySummary(); } else { System.out.println("Student ID not found"); } } else if(change.equals("B")) { System.out.println("Enter Lab number followed by new mark: "); markID = in.nextInt(); newMark = in.nextInt(); if((idx=getStudentIndex(studentID))>0) { labMarks[idx][markID-1] = newMark; calculateWeightedMarks(); displaySummary(); } else { System.out.println("Student ID not found"); } } else if(change.equals("C")) { System.out.println("Enter exam number followed by new mark: "); markID = in.nextInt(); newMark = in.nextInt(); if((idx=getStudentIndex(studentID))>0) { examMarks[idx][markID-1] = newMark; calculateWeightedMarks(); displaySummary(); } else { System.out.println("Student ID not found"); } } in.nextLine(); } }while(!option.equals("B")); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 2 Lnai 9285

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Joao Gama ,Alipio Jorge ,Carlos Soares

1st Edition

3319235249, 978-3319235240

More Books

Students also viewed these Databases questions