0

Below is the java code I have written to read the information and print the following information by creating various classes and methods.

Name:…… (name of student)
Current GPA:…..
Current grade average:…..

Can this be reduced to a single program/class by combining all the classes under a single program i.e without creating different classes?

Student class:

public class Student
{
   private String name;
   private double gpa;

   //methods
   public Student(){}


   public Student(String n,double g)
   {
   name=n;
   gpa=g;
   }
   //set the first name
   public void setName(String n)
   {
    name=n;
   }
    public void setGpa(double g)
   {
    gpa=g;
   }   
   //get methods
   public String getName(){return name;}
   public double getGpa(){return gpa;}
}

Marks class:

public class Marks extends Student
{
   //data
   private int grade[];

   //methods

   public Marks()
   {
   super();
   grade=new int[0];
   }

   public Marks(String n,double g, int k[])
   {
   super(n,g);
   grade=new int[k.length];
   for(int i=0;i<k.length;i++)
      grade[i]=k[i];
   }

   //average method
   public double  average()
   {
   double sum=0;
   for(int i=0;i<grade.length;i++)
   sum+=grade[i];
   return sum/grade.length;
   }  
}

Main class:

import java.io.*;
import java.util.Scanner;

public class Lab
{
   public static void main(String args[]) throws Exception
   {
   //data
   Student list[];  
   String name;
   double gpa;
   int k[], i,j;
   File toRead= new File("input.txt");
   Scanner get=new Scanner(toRead);

   //read input
   list=new Student[get.nextInt()];
   for(i=0;i<list.length;i++)
      {
          get.nextLine();
          name=get.nextLine();
          gpa=get.nextDouble();
          int gn=get.nextInt();
          k=new int[gn];
          for(j=0;j<k.length;j++)
          k[j]=get.nextInt();

          list[i]=new Marks(name,gpa,k); 
      }


   //print all the  name
   for(i=0;i<list.length;i++)
      System.out.printf("Name:%s\nGpa:%.2f\n",list[i].getName(),list[i].getGpa());


   //print average grades for student
   for(i=0;i<list.length;i++)
       {
       if(list[i] instanceof Marks)
         {
         Marks s=(Marks)list[i];
         System.out.printf("average: %s- %.2f\n", s.getName(),s.average());
         }
       }
   }
}

input.txt

3
Mark antony
3.4
10
100 100 90 80 34 56 78 90 89 80
Alex Smith Jr.
2.9
5
100 95 98 78 80
Mary J. John
3.4
12
35 56 100 100 90 98 87 89 88 77 67 95
Apple
  • 35
  • 6
  • 3
    Do you mean you'd like to put all the classes in one source file, or _really_ get rid of all the classes and use only one class? The first can be done, the second makes no sense at all. Please clarify. – Jim Garrison Feb 12 '17 at 06:39
  • 1
    It can. But I think the question is... should you do this? And the answer is no. – Luke Joshua Park Feb 12 '17 at 06:39
  • You might want to simply [package all the classes into an executable jar](http://stackoverflow.com/questions/5258159/how-to-make-an-executable-jar-file). – Andrew Jenkins Feb 12 '17 at 06:40
  • yes i would like to put all the classes in one source file – Apple Feb 12 '17 at 06:45
  • We are asked to write the code without creating multiple classes like I did. – Apple Feb 12 '17 at 06:50
  • can a singe code be written for the above input file to produce the below output: – Apple Feb 12 '17 at 06:53
  • OUTPUT: Name:Mark antony Gpa:3.40 Name:Alex Smith Jr. Gpa:2.90 Name:Mary J. John Gpa:3.40 average: Mark antony- 79.70 average: Alex Smith Jr.- 90.20 average: Mary J. John- 81.83 – Apple Feb 12 '17 at 06:54

1 Answers1

0

You can do nesting of classes. But better way can be Declare a single class as Student and write a functions with respect to Marks which performs the calculations. Method call is always possible from class. This will minimize the code.