mindfreak
mindfreak

Reputation: 455

How to get the total of each column

Its been a ridiculous task especially to build the total of each vertical column & to show it on screen. Though, calculating the total of horizontal row never seemed to be a challenge.

The problem I'm having is three-fold.

  1. How do I calculate the total of each vertical column ?

  2. The index (id) is getting printed in descending order. How do I make it print in ascending order ?

  3. Further, in the percentage column the value after the decimal point is being discarded. How do I get that displayed? For eg.. if answer is supposed to be 78.25% it exhibits as 78.0%

P.S : (2 places after the decimal point is what I'm aiming at.)

POJO class -- StudentsProg.java

package com.students.marks;
import java.util.Arrays;

public class StudentsProg {

    private int id = 0;
    private String name;
    private int english;
    private int german;
    private int french;
    private int arabic;
    private double percentage;
    private int total_marks;
    private int rowHighest;




    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getEnglish() {
        return english;
    }
    public void setEnglish(int english) {
        this.english = english;
    }
    public int getGerman() {
        return german;
    }
    public void setGerman(int german) {
        this.german = german;
    }
    public int getFrench() {
        return french;
    }
    public void setFrench(int french) {
        this.french = french;
    }
    public int getArabic() {
        return arabic;
    }
    public void setArabic(int arabic) {
        this.arabic = arabic;
    }
    public double getPercentage() {
        return percentage;
    }
    public void setPercentage(double percentage) {
        this.percentage = percentage;
    }
    public int getTotal_marks() {
        return total_marks;
    }
    public void setTotal_marks(int total_marks) {   
        this.total_marks = total_marks;
    }

    public int getRowHighest() {
        return rowHighest; 
      }

    public void setRowHighest(int rowHighest) {
        this.rowHighest = rowHighest;
    }


    public String toString() {
        id = id+1; 
        return (id + "\t" +name+ "\t\t" +english+ "\t" + " " +german+ "\t" + " "+ french+ "\t" + " " +arabic+ "\t" +" " +total_marks+ "\t\t" + " " +percentage+ "\t\t" +rowHighest);

    }

}

StudentsProgMain.java

import java.util.Scanner;

public class StudentsProgMain {


    @SuppressWarnings("resource")
    public static void main(String[] args) {

        int count = 0;
        StudentsProg[] stud = new StudentsProg[15]; 

        int choice=0;
        int max = 0;


        Scanner scanner = new Scanner(System.in);

        do{

            System.out.println("1: Add new Student");
            System.out.println("2: List Student");
            System.out.println("3: List Student By Name.");
            System.out.println("4: Delete Student");
            System.out.println("5: Exit");
            System.out.println("Please enter your choice \n\n");
            choice=scanner.nextInt();

        switch(choice){ 

        case 1: 
                stud[count] = new StudentsProg();
                System.out.println("Enter student name");
                stud[count].setName(scanner.next());

                System.out.println("Enter marks in English");
                stud[count].setEnglish(scanner.nextInt());

                System.out.println("Enter marks in German");
                stud[count].setGerman(scanner.nextInt());

                System.out.println("Enter marks in French");
                stud[count].setFrench(scanner.nextInt());

                System.out.println("Enter marks in Arabic");
                stud[count].setArabic(scanner.nextInt());




                count++;

                break;

        case 2: 

            System.out.println("ID\t" + "Name \t\t\t" + "English\t" + " " + "German\t"+ " " + "French\t" + " " + "Arabic\t"
                    +" "+ "Total Marks\t" + " " + "Percentage\t" + "Highest Marks(Row)\n" +
                    "------------------------------------------------------------------------"
                    + "------------------------------------------- \n ");


            for(int i=0; i<count; i++){
                if(stud[i]!=null){

                  int total_marks = stud[i].getEnglish()+stud[i].getGerman()+ stud[i].getFrench()+stud[i].getArabic();
                  stud[i].setTotal_marks(total_marks);

                  double calc_per = ((total_marks*100)/400);

                  stud[i].setPercentage(calc_per);

                  int arrayListMarks [] = {stud[i].getEnglish(), stud[i].getFrench(), stud[i].getGerman(), stud[i].getArabic()};

                  max  = arrayListMarks[0];

                        for (int j = 1; j < arrayListMarks.length; j++) {
                            if(arrayListMarks[j] >  max)
                                max = arrayListMarks[j]; }

                  stud[i].setRowHighest(max);

                  System.out.println(stud[i].toString());
                  System.out.println("\n");}

            } 

            System.out.println("--------------------------------------------------------------"
                    + "----------------------------------------------------- \n");

             System.out.println("\tTotal :"  +"\n");

             break;   


        case 3 : 

            System.out.println("Please enter your name");
            String name = scanner.next();

            System.out.println("\n" + "ID\t" + "Name \t\t\t" + "English\t" + " " + "German\t"+ " " + "French\t" + " " + "Arabic\t"
                    +" "+ "Total Marks\t" + " " + "Percentage\t" + "Highest Marks(Row)\n" +
                    "------------------------------------------------------------------------"
                    + "------------------------------------------- \n ");

            for(int i =0 ; i<count; i++){
                if(stud[i]!=null && stud[i].getName().equals(name))
                    System.out.println(stud[i].toString()); }

            System.out.println("--------------------------------------------------------------"
                    + "----------------------------------------------------- \n");

            break;


        case 4 :
            System.out.println("Please enter your name");
            String naam = scanner.next();

            for (int i = 0; i<count; i++) {
                if(stud[i]!=null && stud[i].getName()==naam)
                    stud[i]=null;
            }
            break;

        case 5: 
            System.exit(0);
            System.out.println("You have exited successfully");

        default :
            System.out.println("Invalid choice");

    }


    }while(true);
 }

}   

Upvotes: 0

Views: 137

Answers (1)

redge
redge

Reputation: 1182

The problem with the percentage calculation is that the line of code double calc_per = ((total_marks*100)/400); will do integer arithmetic and truncate each intermediate result as an integer. To fix this you need to include a floating point number somewhere in the calculation either by converting total_marks to double like so:

     double calc_per = ((Integer.valueOf(total_marks).doubleValue()*100)/400);    

Or using a float constant like so:

     double calc_per = ((total_marks*100.0)/400);

Vertical totals should just be a matter of adding the row value to a variable inside your print loop.

I'm not really sure about your index order problem but the code in toString() that reads id = id+1; looks wrong. This will increment the Id each time you call toString(). Instead of that, your creation code should set the value of id just after creating the object, something like:

            stud[count] = new StudentsProg();
            // add the following line of code.
            stud[count].setId(count);
            System.out.println("Enter student name");
            stud[count].setName(scanner.next()); 

Upvotes: 1

Related Questions