12412 - A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

All about problems in Volume 124. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
laituanksa245
New poster
Posts: 20
Joined: Tue Jan 10, 2012 4:23 pm
Location: Vietnam

12412 - A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

Post by laituanksa245 »

Can someone help me figure out what is wrong with my code ?
This problem seems easy but long. I spent like almost 3 hours to make it. However, I kept on getting WA :(
Help me please.

Code: Select all

import java.util.Scanner;
import java.text.*;

class Student {
    long CID;
    String SID;
    String name;
    int Chinese,Math, English, Programming;
    public Student (String name1, String SID1, long CID1, int Chinese1, int Math1, int English1,int Programming1 )
    {
        name = name1;
        SID = SID1;
        CID = CID1;
        Chinese = Chinese1;
        Math = Math1;
        English = English1;
        Programming = Programming1;
    }
    public int getTotalScore ()
    {
        return Chinese + Math + English + Programming;
    }
    public double getAverageScore ()
    {
        double rs =  ((double) getTotalScore ())/4.0;
        return rs;
    }
    public String toString ()
    {
        String rs = "";
        DecimalFormat df = new DecimalFormat("##.00");
        rs = rs + SID + " " + CID + " " + name + " " + Chinese + " " + Math + " " + English + " " + Programming + " " + getTotalScore () + " " + df.format(getAverageScore ());
        return rs;
    }
    public String getSID ()
    {
        return SID;
    }
}
public class Main {
    public static void MainMenu ()
    {
        System.out.println ("Welcome to Student Performance Management System (SPMS).");
        System.out.println ("");
        System.out.println ("1 - Add");
        System.out.println ("2 - Remove");
        System.out.println ("3 - Query");
        System.out.println ("4 - Show ranking");
        System.out.println ("5 - Show Statistics");
        System.out.println ("0 - Exit");
        System.out.println ("");
    }
    public static void main (String [] args)
    {
        String name;
        DecimalFormat df = new DecimalFormat("##.00");
        long CID;
        String SID;
        int Chinese,Math,Programming,English;
        Student [] StudentList = new Student [100];
        int NbOfStudent = 0;
        String input;
        Scanner kb = new Scanner (System.in);
        int option;
        while (true)
        {
            MainMenu ();
            option = kb.nextInt ();
            if (option == 0)
            {
                System.exit (0);
            }
            else
            if (option == 1)
            {
                while (true)
                {
                    System.out.println ("Please enter the SID, CID, name and four scores. Enter 0 to finish.");
                    SID = "";
                    while (SID.equals (""))
                        SID = kb.next ();
                    if (SID.equals ("0"))
                        break;
                    CID = kb.nextLong ();
                    name = "";
                    while (name.equals (""))
                        name = kb.next ();
                    Chinese = kb.nextInt ();
                    Math = kb.nextInt ();
                    Programming = kb.nextInt ();
                    English = kb.nextInt ();
                    boolean Duplicated = false;
                    for (int i = 0; i < NbOfStudent; i++)
                        if (StudentList[i].getSID ().equals(SID))
                        {
                            Duplicated = true;
                            break;
                        }
                    if (Duplicated)
                        System.out.println ("Duplicated SID.");
                    else
                    {
                        StudentList [NbOfStudent] = new Student (name, SID, CID, Chinese,Math, Programming, English );
                        NbOfStudent ++;
                    }
                }
                
            }
            else
            if (option == 2)
            {
                while (true)
                {
                    System.out.println ("Please enter SID or name. Enter 0 to finish.");
                    String query = "";
                    while (query.equals (""))
                        query = kb.nextLine ();
                    if (query.equals ("0"))
                        break;
                    int xx = 0;
                    int i = 0;
                    while (i < NbOfStudent)
                    {
                        if (StudentList[i].name.equals (query) || StudentList[i].SID.equals (query))
                        {
                            xx ++;
                            for (int j = i; j < NbOfStudent - 1; j++)
                                StudentList[j] = new Student (StudentList[j+1].name,StudentList[j+1].SID, StudentList[j+1].CID, StudentList[j+1].Chinese, StudentList[j+1].Math, StudentList[j+1].English, StudentList[j+1].Programming);
                            NbOfStudent = NbOfStudent - 1;
                        }
                        else
                            i++;
                    }
                    System.out.println (xx + " student(s) removed.");
                }
            }
            else
            if (option == 3)
            {
                while (true)
                {
                    System.out.println ("Please enter SID or name. Enter 0 to finish.");
                    String query;
                    query = "";
                    while (query.equals (""))
                        query = kb.nextLine ();
                    if (query.equals ("0"))
                        break;
                    for (int i = 0; i < NbOfStudent; i++)
                        if (StudentList[i].name.equals (query) || StudentList[i].getSID().equals (query))
                        {
                            int rank = 1;
                            for (int j = 0; j < NbOfStudent; j++)
                                if (StudentList[j].getTotalScore() > StudentList[i].getTotalScore ())
                                    rank ++;
                            System.out.println (rank + " " + StudentList[i]);
                        }
                }
                
            }
            else
            if (option == 4)
            {
                System.out.println ("Showing the ranklist hurts students' self-esteem. Don't do that.");
            }
            else
            if (option == 5)
            {
                System.out.println ("Please enter class ID, 0 for the whole statistics.");
                int classID;
                classID = kb.nextInt ();
                int totalStudent = 0;
                int totalMath = 0;
                int totalChinese = 0;
                int totalEnglish = 0;
                int totalProgramming = 0;
                int passChinese = 0;
                int failChinese = 0;
                int passMath = 0;
                int failMath = 0;
                int passEnglish = 0;
                int failEnglish = 0;
                int passProgramming = 0;
                int failProgramming = 0;
                int pass4 = 0;
                int pass3 = 0;
                int pass2 = 0;
                int failAll = 0;
                int pass1 = 0;
                for (int i = 0; i < NbOfStudent; i++)
                if (classID == 0 || classID == StudentList[i].CID)
                {
                    totalStudent ++;
                    Math = StudentList[i].Math;
                    English = StudentList[i].English;
                    Programming = StudentList[i].Programming;
                    Chinese = StudentList[i].Chinese;
                    totalMath = Math + totalMath;
                    totalEnglish = English + totalEnglish;
                    totalProgramming = Programming + totalProgramming;
                    totalChinese = Chinese + totalChinese;
                    int totalPass = 0;
                    if (Chinese >= 60)
                    {
                        totalPass ++;
                        passChinese ++;
                    }
                    else
                    {
                        failChinese++;
                    }
                    if (Math >= 60)
                    {
                        totalPass ++;
                        passMath ++;
                    }
                    else
                    {
                        failMath++;
                    }
                    if (English >= 60)
                    {
                        totalPass ++;
                        passEnglish ++;
                    }
                    else
                    {
                        failEnglish++;
                    }
                    if (Programming >= 60)
                    {
                        totalPass ++;
                        passProgramming ++;
                    }
                    else
                    {
                        failProgramming ++;
                    }
                    if (totalPass == 0)
                        failAll ++;
                    if (totalPass >= 1)
                        pass1 ++;
                    if (totalPass >= 2)
                        pass2 ++;
                    if (totalPass >= 3)
                        pass3 ++;
                    if (totalPass >= 4)
                        pass4 ++;
 
                    
                }
                double AverageSubject;
                //Chinese
                System.out.println ("Chinese");
                System.out.print   ("Average Score: ");
                if (totalStudent != 0)
                {
                    AverageSubject = ((double)totalChinese)/ ((double)totalStudent);
                    System.out.print (df.format (AverageSubject));
                }
                else
                    System.out.println ("0.00");
                System.out.println ();
                System.out.println ("Number of passed students: " + passChinese);
                System.out.println ("Number of failed students: " + failChinese);
                System.out.println ();
                //Mathematics
                System.out.println ("Mathematics");
                System.out.print   ("Average Score: ");
                if (totalStudent != 0)
                {
                    AverageSubject = ((double)totalMath)/ ((double)totalStudent);
                    System.out.print (df.format (AverageSubject));
                }
                else
                {
                    AverageSubject = 0;
                    System.out.print ("0.00");
                }
                System.out.println ();
                System.out.println ("Number of passed students: " + passMath);
                System.out.println ("Number of failed students: " + failMath);
                System.out.println ();
                //English
                System.out.println ("English");
                System.out.print   ("Average Score: ");
                if (totalStudent != 0)
                {
                    AverageSubject = ((double) totalEnglish)/ ((double)totalStudent);
                    System.out.print (df.format (AverageSubject));
                }
                else
                    System.out.println ("0.00");
                System.out.println ();
                System.out.println ("Number of passed students: " + passEnglish);
                System.out.println ("Number of failed students: " + failEnglish);
                System.out.println ();
                //Programming
                System.out.println ("Programming");
                System.out.print   ("Average Score: ");
                if (totalStudent != 0)
                {
                    AverageSubject = ((double)totalProgramming)/ ((double)totalStudent);
                    System.out.print (df.format (AverageSubject));
                }
                else
                    System.out.println ("0.00");
                System.out.println ();
                System.out.println ("Number of passed students: " + passProgramming);
                System.out.println ("Number of failed students: " + failProgramming);
                System.out.println ();
                //Overall
                System.out.println ("Overall:");
                System.out.println ("Number of students who passed all subjects: " + pass4);
                System.out.println ("Number of students who passed 3 or more subjects: " + pass3);
                System.out.println ("Number of students who passed 2 or more subjects: " + pass2);
                System.out.println ("Number of students who passed 1 or more subjects: " + pass1);
                System.out.println ("Number of students who failed all subjects: " + failAll);
                System.out.println ();
                
            }
        }
    }
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 12412 WA A Typical Homework (a.k.a Shi Xiong Bang Bang M

Post by brianfry713 »

Check input and AC output for thousands of problems on uDebug!
laituanksa245
New poster
Posts: 20
Joined: Tue Jan 10, 2012 4:23 pm
Location: Vietnam

Re: 12412 WA A Typical Homework (a.k.a Shi Xiong Bang Bang M

Post by laituanksa245 »

I removed "public" but I still got WA :(
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 12412 WA A Typical Homework (a.k.a Shi Xiong Bang Bang M

Post by brianfry713 »

The 4th line of your code should be class Main instead of class Student, Post the complete updated code if you still want help.
Check input and AC output for thousands of problems on uDebug!
tobygameac
New poster
Posts: 2
Joined: Thu Aug 23, 2012 3:33 pm

Re: 12412 WA A Typical Homework (a.k.a Shi Xiong Bang Bang M

Post by tobygameac »

It's hard to debug in this problem, could anybody give me some critical case of this problem?
Thank you very much.

Code: Select all

AC
Last edited by tobygameac on Sat Aug 24, 2013 7:05 am, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 12412 WA A Typical Homework (a.k.a Shi Xiong Bang Bang M

Post by brianfry713 »

Try adding 1e-5 to all floating point values as you print them.
Check input and AC output for thousands of problems on uDebug!
tobygameac
New poster
Posts: 2
Joined: Thu Aug 23, 2012 3:33 pm

Re: 12412 WA A Typical Homework (a.k.a Shi Xiong Bang Bang M

Post by tobygameac »

Didn't even think about this problem, thank you so much.
Post Reply

Return to “Volume 124 (12400-12499)”