10 Replies - 352 Views - Last Post: 02 August 2013 - 03:04 AM Rate Topic: -----

#1 Yer_Da  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-July 13

Creating a new object in another class and displaying data

Posted 01 August 2013 - 01:17 AM

Hi there, as part of an assignment, I have to create a new Result object in my student class and then display it within my gradesTest class. I have created the object but I'm having trouble displaying the set object data in gradesTest. The end result is to have the student and result information - name,banner,coursework,exam,totalMark,grade and result displayed in a tabular format via gradesTest, I've set up the table fine and can see the information for name and banner but displaying the result data is causing me some problems. Here is my student, result,module and gradesTest class.

public class Student{

String name,banner;
public Student(String name,String banner){
    this.name=name;
    this.banner=banner;
    Result student1=new Result(50.4,64.3,(50.4+64.3)/2,"B2","PASS");
}
@Override
public String toString(){
        String studentDetails=new String();
        studentDetails+=String.format("%-20s%10s",name,banner);
        return studentDetails;
    }
}


public class Result {
    String grade,result;
    double coursework,exam,totalMark;
    
    
    
public Result (double coursework,double exam,double totalMark,String grade,String result){
    this.coursework=coursework;
    this.exam=exam;
    this.totalMark=totalMark;
    this.grade=grade;
    this.result=result;
    }

public double getTotalMark(double coursework,double exam)
{
    double totalMark=(coursework+exam)/2;
    return totalMark;
    
}
    
public String getGrade(double totalMark,double coursework, double exam)
    
    {
    if (totalMark>69.5 && coursework>=40 && exam>=40)
        
        {
            grade= "A";
        }

        else if (totalMark>=59.5 && totalMark<=69.4 && coursework>=40 && exam>=40)
        {
            grade= "B1";

        }

        else if (totalMark>=49.5 && totalMark<=59.4 && coursework>=40 && exam>=40)

        {

            grade="B2";

        }

        else if (totalMark>=39.5 && totalMark<=49.4 && coursework>=40 && exam>=0)

        {

            grade="C";

        }

        else if (totalMark>=29.5 && totalMark<=39.4 && coursework>=40 && exam>=40)

        {

            grade="D";

        }
        
        else if (totalMark<29.5 && coursework>=40 && exam>=40)

        {

            grade="E";

        }
        
        else if (coursework<40 && exam >=40)

        {

            grade="RC2";

        }
        
        else if (coursework>=40 && exam <40)

        {

            grade="RE2";
        }
        return "See Lecturer";
}
    /**
     *
     * @param grade
     * @param result
     * @return
     */
    public String getResult(String grade)
    {
        switch (grade) {
            case "A":
                result="PASS";
                break;
            case "B1":
                result="PASS";
                break;
            case "B2":
                result="PASS";
                break;
            case "C":
                result="PASS";
                break;
            case "D":
                result="FAIL";
                break;
            case "E":
                result="FAIL";
                break;
            case "RC2":
                result="FAIL";
                break;
            case "RE2":
                result="FAIL";
                break;
        }
        return "See Lecturer";
       }
}


public class Module {
   String moduleName,pctCwork,pctExam;
   private Student[] students;
   private int numberofStudents;
   
   
public Module(int numberofStudents) {

this.students=new Student[numberofStudents];

}

public void addStudent(String name,String banner){
    this.students[this.numberofStudents]=new Student(name,banner);
    this.numberofStudents++;
}

   @Override
   public String toString(){
        String moduleDetails=new String();
        moduleDetails+=String.format("%-20s%7s%15s%15s%15s%15s%15s\n","NAME","BANNER","CWORK","EXAM","TOTAL","GRADE","RESULT");
        for(int student=0;student<this.numberofStudents;student++){
            moduleDetails+=this.students[student].toString()+"\n";
        }
        
        return moduleDetails;
}
}


public class GradesTest {
    public static void main(String[] args) {
        final int MAX_NUMBER_OF_STUDENTS=10;
        Module module=new Module(MAX_NUMBER_OF_STUDENTS);
        module.addStudent("RICHARD O'DONNELL","B00166353");
        module.addStudent("ADRIAN SMITH","B00270257");
        module.addStudent("KIEFER SUTHERLAND","B00211266");
        System.out.println(module);


Is This A Good Question/Topic? 0
  • +

Replies To: Creating a new object in another class and displaying data

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Creating a new object in another class and displaying data

Posted 01 August 2013 - 02:59 AM

Define "having trouble." How can we help?

Consider rewriting your switch statement so arguments with the same result are combined:
switch ( argument )
{
    case "this":
    case "that":
    {
        // the common result for this and that goes here
        break;
    }
}

Was This Post Helpful? 1
  • +
  • -

#3 Yer_Da  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-July 13

Re: Creating a new object in another class and displaying data

Posted 01 August 2013 - 03:30 AM

The doubles and strings from the new Result object created in Student will not display in gradesTest. I tried crearing a similar toString method in Result but it would not work.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Creating a new object in another class and displaying data

Posted 01 August 2013 - 03:37 AM

You will need to make tour student1 variable an instance variable
so toString() can access it

public class Student{
String name,banner;
Result student1;       // <----- declare here

public Student(String name,String banner){
    this.name=name;
    this.banner=banner;
    student1=new Result(50.4,64.3,(50.4+64.3)/2,"B2","PASS");  // <--- define here
}
@Override
public String toString(){
        String studentDetails=new String();
        studentDetails+=String.format("%-20s%10s",name,banner) +
            student1.xxxxx();   // <--- can be accessed here
        return studentDetails;
    }
}


This post has been edited by pbl: 01 August 2013 - 03:37 AM

Was This Post Helpful? 1
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Creating a new object in another class and displaying data

Posted 01 August 2013 - 03:40 AM

You create a LOCAL Result object, called student1, for every Student created - all exactly the same. First, you probably don't want to do it that way, but perhaps you're building to the final solution. At a minimum, make the Result object an instance variable.

Then, if you want to include the Result object in the student object's results, as part of the toString() method, then you should add the desired parts of the student1 object to the toString() method. I don't see where you've tried to do anything with it. Well, you'd have trouble if you did the way your Student class is currently built.
Was This Post Helpful? 1
  • +
  • -

#6 Yer_Da  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-July 13

Re: Creating a new object in another class and displaying data

Posted 01 August 2013 - 03:52 AM

Yea, I want a different result object for every student but as you say, I'm building up to that. Thanks very much for your help guys, I'll try your suggestions when I'm home from work and let you know.

Thanks again!
Was This Post Helpful? 0
  • +
  • -

#7 Yer_Da  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-July 13

Re: Creating a new object in another class and displaying data

Posted 01 August 2013 - 04:33 PM

The good news is, I can now see the result details when I run gradesTest. Do I create three separate result objects for each student or is there a way to have java create a new result object for every new student object?
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Creating a new object in another class and displaying data

Posted 01 August 2013 - 04:46 PM

Is there a way? It's your program and it's Java, so there's a way to do most anything you want to do.

If you want a unique Result object that belongs to each Student object (a Result instance variable), the Student() constructor is a good place to build the Result object. Change the constants currently being passed to the Result() constructor to variables specific to that instance of the Student object.
Was This Post Helpful? 0
  • +
  • -

#9 Yer_Da  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-July 13

Re: Creating a new object in another class and displaying data

Posted 01 August 2013 - 05:20 PM

I tried this but still can't break it down to each individual student having individual results.

package B00166353_Grades;

public class Student{
String name,banner;
Result student1;
public double coursework;
public double exam;
public double totalMark;
public String grade;
public String result;


public Student(String name,String banner){
    this.name=name;
    this.banner=banner;
    student1=new Result(coursework,exam,totalMark,grade,result);
}
 


 @Override
public String toString(){
        String studentDetails=new String();
        studentDetails+=String.format("%-20s%10s%12s%15s%13s%15s%15s",name,banner,student1.coursework,student1.exam,student1.totalMark,student1.grade,student1.result);
        return studentDetails;
    }
}

Was This Post Helpful? 0
  • +
  • -

#10 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Creating a new object in another class and displaying data

Posted 01 August 2013 - 09:48 PM

You can add a result object to your student object in the parameters to give each student its own results.
Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Creating a new object in another class and displaying data

Posted 02 August 2013 - 03:04 AM

Quote

I tried this but still can't break it down to each individual student having individual results.

The Result object, student1, in each Student object is actually unique, but they look the same because the values they contain are the same. This statement in the Student() constructor

student1=new Result(coursework,exam,totalMark,grade,result);

sends the same values, 0.0, 0.0, 0.0, null, null, to the Result() constructor, because those are the values of those variables at that point in the Student() constructor.

Which results in a sample run:
NAME                 BANNER          CWORK           EXAM          TOTAL          GRADE         RESULT
RICHARD O'DONNELL    B00166353         0.0            0.0          0.0           null           null
ADRIAN SMITH         B00270257         0.0            0.0          0.0           null           null
KIEFER SUTHERLAND    B00211266         0.0            0.0          0.0           null           null

So, the question is, how do you change those values for each student BEFORE the Result object is created?

Ignoring all of that for a moment, what were your thoughts about how those values would change? How were the variables coursework, exam, totalMark, grade, and result to be entered or modified for each student? Once you answer that, you can modify the design to include that unique student info in the creation of each student's Result object.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1