Click here to Skip to main content
16,017,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
main.java 

    import java.io.File;
    
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class Main {
    
    	private static String[] name;
    
    public static boolean readFile(String filename) {
    	File file = new File("studentdata.txt");
        try {
        	Scanner scan = new Scanner(file); 
    		while (scan.hasNextLine() ) {
    			System.out.println(scan.nextLine().split(","));
    		
    			addNewStudent(id, firstName, lastName, mathsMark1, mathsMark2, mathsMark3, englishMark1, englishMark2, englishMark3); 
    			}
    scan.close();
    
    }
    
    catch (FileNotFoundException e) 
    { System.out.println("Failedto readfile");
    }
    return true;
    }
    
     
    private static void addStudent(int id, String firstName, String lastName,int mathsMark1, int mathsMark2, int mathsMark3, int englishMark1, int englishMark2, int englishMark3) {
    
    	
    	Student student = new Student(id, firstName, lastName, mathsMark1, mathsMark2, mathsMark3, englishMark1, englishMark2, englishMark3);
    	
        
    	AssessmentMarks mathMarks = new AssessmentMarks("Mathematics", mathsMark1, mathsMark2, mathsMark3);
        AssessmentMarks englishMarks = new AssessmentMarks("englishs", englishMark1, englishMark2,englishMark3);
        
       
        student.mathsMarks = mathMarks;
        student.englishMarks = englishMarks;
        
      List<Student> students = new ArrayList<>();
        students.add(student);`enter code here`
    }
    
    
    private static void displayReportByMarks() {
    	
    	System.out.printf("Name " +  " " + " " + "Maths " + " " + "A1" + " " + "A2" + " " + "A3" + " " + "Mark " + " " + "English" + " " + "A1" + " " + "A2" + " " + "A3" + "Mark", "\n ");
    	System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------\n");
    	System.out.printf("getFullName " +  " " + " " + " " + " " + "getMark(mathsMark1)" + " " + "getMark(mathsMark2)" + " " + "getMark(mathsMark3)" + " " + "getAverageMark(mathsMark1, mathsMark2, mathMark3)" + " " + " " + " " + "getMark(englishMark1)" + " " + "getMark(englishMark2)" + " " + "getMark(englishMark3)" + " " + "getAverageMark(englishMark1, englishMark2, englishMark3)", "\n ");
    	
    }
    
    
    private static void displayReportByGrades() {
    	
    	System.out.printf("Name " +  " " + " " + "Maths " + " " + "A1" + " " + "A2" + " " + "A3" + " " + "Grade " + " " + "English" + " " + "A1" + " " + "A2" + " " + "A3" + "Grade", "\n ");
    	System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------\n");
    	System.out.printf("getFullName " +  " " + " " + " " + " " + "getGrade(mathsMark1)" + " " + "getGrade(mathsMark2)" + " " + "getGrade(mathsMark3)" + " " + "getAverageGrade(getAverageMark(mathsMark))" + " " + " " + " " + "englishMark1" + " " + "englishMark2" + " " + "englishMark3" + " " + "getAverageGrade(getAverageMark(englishMarks))" , "\n ");
    	
    	
    	}
    
    
    private static void displayMenu() {
    
    	System.out.println("Student Report System");
    	System.out.println("-----------------------\n");
    	System.out.println("1) Display student marks");
    	System.out.println("2) Display student grades");
    	System.out.println("3) Add new student");
    	System.out.println("4) Remove student");
    	System.out.println("5) Exit");
    	System.out.println("");
    	System.out.println("Your option selection is: ");
    	System.out.println("");
    }
    
    
    private static int selectMenuOption() {
    	int userSelected ;
    	do {
    		userSelected = MenuData();
    		switch(userSelected) {
    			case 1:
    				System.out.println("");
    				userSelected1();
    				break;
    			case 2:
    				System.out.println("");
    				userSelected2();
    				break;
    			case 3:
    				System.out.println("");
    				userSelected3();
    				break;
    			case 4:
    				System.out.println("");
    				userSelected4();
    				break;
    			case 5:
    				System.out.println("");
    				userSelected5();
    				break;
    				default:
    					System.out.println("");
    					System.out.println("Invalid selection, please enter an integer value between 1 and 5");
    					System.out.println("");
    
    					break;
    		}
    	}
    	while(userSelected >5);
    	
    
    }
    
    
    private static int MenuData() {
    	
    	return 0;
    }
    
    private static void userSelected5() {
    	Exit();
    	
    }
    
    private static String Exit() {;
    	return("Good Bye");
    	
    }
    
    private static void userSelected4() {
    	
    	removeStudent() ;
    }
    
    private static void userSelected3() {
    	
    	addNewStudent();
    }
    
    private static void userSelected2() {
    	displayReportByGrades();
    	
    }
    
    private static void userSelected1() {
    	displayReportByMarks();
    	
    }
    
    private static void addNewStudent() {
    	
    
    	int id;
    	String firstName;
    	String lastName;
    	int mathsMark1;
    	int mathsMark2;
    	int mathsMark3;
    	int englishMark1;
    	int englishMark2;
    	int englishMark3;
    	
    	Scanner SC= new Scanner(System.in);
    	System.out.print("Enter ID: ");
        id=SC.nextInt();
        System.out.print("Enter First Name: ");
        firstName=SC.nextLine();
        System.out.print("Enter Last Name: ");
        lastName=SC.nextLine();
        System.out.print("Enter math mark1: ");
        mathsMark1=SC.nextInt();
        System.out.print("Enter math mark2: ");
        mathsMark2=SC.nextInt();
        System.out.print("Enter math mark3: ");
        mathsMark3=SC.nextInt();
        System.out.print("Enter english mark1: ");
        englishMark1=SC.nextInt();
        System.out.print("Enter english mark2: ");
        englishMark2=SC.nextInt();
        System.out.print("Enter english mark3: ");
        englishMark3=SC.nextInt();
        
    
    
    	Student student = new Student(id, firstName, lastName);
    	
     
    	AssessmentMarks mathMarks = new AssessmentMarks("Mathematics", mathsMark1, mathsMark2, mathsMark3);
     AssessmentMarks englishMarks = new AssessmentMarks("englishs", englishMark1, englishMark2,englishMark3);
     
    
     student.mathsMarks = mathMarks;
     student.englishMarks = englishMarks;
     
    List<Student> students = new ArrayList<>();
     students.add(student);
    
    
    
    
     private static void removeStudent(){
    private static <id> void removeStudent(id, studentToBeRemoved) {
    	int id;
    	
    
    
    Scanner SC= new Scanner(System.in);
    	System.out.print("Enter ID of the student you want to delete ");
    	id=SC.nextInt();
    
    	 int id = studentToBeRemoved.getId();
    
         id current = head;
         if (head.getId() == id) {
             head = head.getNext();
             size--;
             return true;
         }
         while (current.getNext() != null) {
             id next = current.getNext();
    
             if (next.getId() == id) {
                 current.setNext(next.getNext());
                 size--;
                 return true;
             }
             current = next;
         }
         return false;
     }



AssessmentMarks.java

   

public class AssessmentMarks {


private String courseName;
private int assessment1;
private int assessment2;
private int assessment3;
private double mark;
private String userGrade;



   
public AssessmentMarks(String name, int mark1, int mark2, int mark3) {
    courseName =  name;
    assessment1 = mark1;
    assessment2 = mark2;
    assessment3 = mark3;
   }



   

public void setMark(int assessmentNumber, int mark) {            

	this.mark = assessmentNumber;
	
}
	
   
public int getMark(int assessmentNumber) {
	return assessmentNumber ;
	
}


public int getAverageMark() {
	return (assessment1 + assessment2 + assessment3) / 3;
	
}


public String getGrade(int assessmentNumber) {

	if (assessmentNumber < 40)                     
		System.out.println("D");
	if ((assessmentNumber >= 41) && (assessmentNumber < 49))    
		System.out.println("C-");
	if ((assessmentNumber >= 50) && (assessmentNumber < 59))  
		System.out.println("C");
	if ((assessmentNumber >= 60) && (assessmentNumber < 69))  
		System.out.println("C+");
	if ((assessmentNumber >= 70) && (assessmentNumber < 74))  
		System.out.println("B-");
	if ((assessmentNumber >= 75) && (assessmentNumber < 79))  
		System.out.println("B");
	if ((assessmentNumber >= 80) && (assessmentNumber < 84))
		System.out.println("B+");
	if ((assessmentNumber >= 85) && (assessmentNumber < 89))  
		System.out.println("A-");
	if ((assessmentNumber >= 90) && (assessmentNumber < 94))  
		System.out.println("A");
	if ((assessmentNumber >= 95) && (assessmentNumber <= 100))  
		System.out.println("A+");
	return getGrade(assessmentNumber);
	
}


    public String getAverageGrade() {
    	double averageMark = getAverageMark();
    	if (averageMark < 40)                     
    		System.out.println("D");
    	if ((averageMark >= 41) && (averageMark < 49))    
    		System.out.println("C-");
    	if ((averageMark >= 50) && (averageMark < 59))  
    		System.out.println("C");
    	if ((averageMark >= 60) && (averageMark < 69))  
    		System.out.println("C+");
    	if ((averageMark >= 70) && (averageMark < 74))  
    		System.out.println("B-");
    	if ((averageMark >= 75) && (averageMark < 79))  
    		System.out.println("B");
    	if ((averageMark >= 80) && (averageMark < 84))  
    		System.out.println("B+");
    	if ((averageMark >= 85) && (averageMark < 89))  
    		System.out.println("A-");
    	if ((averageMark >= 90) && (averageMark < 94))  
    		System.out.println("A");
    	if ((averageMark >= 95) && (averageMark <= 100))  
    		System.out.println("A+");
    	return getAverageGrade();
    		
    }

   

     public String markToGrade(int mark) {
        	
        	//convert student marks into letter grade
        	if (mark < 40)                     
        		System.out.println("D");
        	if ((mark >= 41) && (mark < 49))    
        		System.out.println("C-");
        	if ((mark >= 50) && (mark < 59))  
        		System.out.println("C");
        	if ((mark >= 60) && (mark < 69))  
        		System.out.println("C+");
        	if ((mark >= 70) && (mark < 74))  
        		System.out.println("B-");
        	if ((mark >= 75) && (mark < 79))  
        		System.out.println("B");
        	if ((mark >= 80) && (mark < 84))  
        		System.out.println("B+");
        	if ((mark >= 85) && (mark < 89))  
        		System.out.println("A-");
        	if ((mark >= 90) && (mark < 94))  
        		System.out.println("A");
        	if ((mark >= 95) && (mark <= 100))  
        		System.out.println("A+");
        	
        	return markToGrade(mark);
        } 


    public String CourseName (String name) {
    	 return courseName = name;
    	
    }



    public String getCourseName () {
    	return courseName;
    	
    }
    	
		

	
	student.java 

    class Student {
    	
    				
    		private int id;
    		private String firstName;
    		private String lastName;
    		public AssessmentMarks mathsMarks;
    		public AssessmentMarks englishMarks;
    		
    				
    		public Student(int id, String firstName, String lastName) {
    			this.id = id;
    			this.firstName = firstName;
    			this.lastName = lastName;
    			
    		}
    		
    		
    		public String getFullName() {
    			
    			return firstName + " " + lastName;
    			
    		}
    	}


What I have tried:

design and build a console application that manages a student database. This application will store student information along with their assessment marks. The user will be shown a menu that allows them to add students, remove students, and display a report that shows the student's grade equivalent to their mark. The application will read a text file ‘studentdata.txt’ containing the data for several students. The data will be stored using classes and abstract data types.

I create three main, student, and assessmentmarks. not sure about the programme construction. any idea why it is not compiled and do I create classes correctly?
Posted
Updated 2-Sep-22 21:56pm

That's because your teacher helpfully added markers where you need to provide code:
Java
students.add(student);`enter code here`
The `enter code here` bit is not executable Java code but an instruction to you ...

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

And do yourself a favour: particularly when you are getting started, write a little code, compile it, fix any syntax errors, test it, debug it, fix it, and get it working before you write more code. Typing away until you have ~400 lines of code and then trying to compile it is a recipe for a lot of errors - some of which may mean you have to rethink what you are trying to do ...
 
Share this answer
 
In your main method you have the following lines:
Java
    private static String[] name;

public static boolean readFile(String filename) {
    File file = new File("studentdata.txt");
    try {
        Scanner scan = new Scanner(file);
        while (scan.hasNextLine() ) {
            System.out.println(scan.nextLine().split(","));

            addNewStudent(id, firstName, lastName, mathsMark1, mathsMark2, mathsMark3, englishMark1, englishMark2, englishMark3);
            }
scan.close();

Which is a static method inside a static method; and that is not allowed in Java. Also you are passing filename in to the readFile method but you use a hardcoded name instead of this parameter. You then try to read each line of the file, print all the fields of the line but do not save them. You then call addNewStudent with a list of parameter names that do not even exist.

I suggest you throw this code away and start again.

Start by writing just the code to read the file and split it into its constituent parts. Then create the indiviadual parameter variables as required by the assignment. Write a addNewStudent method which just prints the values of the parameters. Once you have that working you can think about how the addNewStudent method would actually save the details into its database.

Also, if you are using printf rather than println, then make use of printf's formatting features rather than simple string concatenation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900