• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

creating objects of type A as attribute of type B

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...i have this problem i am finding difficult to solve.How do i create a Result object (with attributes courseworkMark, examMark) to return grade and result in a Student class?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand the question.

If you know how to create a Result object, then you know how to do it inside Student class or any other class. Everything you do in Java will happen inside a class.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Result class has the following attributes, double courseworkMark, double examMark,String grade,String result. Now, i need to create a Result object in a Student class that will hold all these variables(of type Result) for a student object.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

First of all, do you know how to create a class; did you already code the Result class? If not, you need to do that first. This tutorial explains how to create a class: Classes.

Then you'll need to create a Result object. You do that using the new operator. See Creating Objects for a tutorial on how to create objects.

You'll want to add a constructor to your Result class, so that you can supply values when you create a Result object.

If you get stuck, it helps if you explain as exactly as possible what you don't understand. Show us the code that you wrote, and try to explain in detail where you are stuck.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Anderson wrote:The Result class has the following attributes, double courseworkMark, double examMark,String grade,String result. Now, i need to create a Result object in a Student class that will hold all these variables(of type Result) for a student object.



That sounds the same as your first post, so all I can suggest is to re-read my first answer.

Have you ever created any object at all? If so, then you know how to create any object in any other class. If not, then you need to study the "Classes and Object" or similar section of your preferred tutorial or text.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i know how to create classes and objects>

Result myResult = new Result(courseworkMark,double examMark);

public void calculateResult(){
overallMark = courseworkMark+examMark;
totalMark=overallMark/2;
if (totalMark>39.5){
grade="A";
result="pass"
} else {
grade="Loser";
result="Failure";
}}

what i want to know is,after i create the Result object in the Student class,how do i get my Student objects to each have their own result? (please understand this time

 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is my code..hope it's not too long>
public class Result {

double courseworkMark;
double examMark;
double overallMark;
double totalMark;
String grade;
String result;

public Result(double courseworkMark,double examMark){
this.courseworkMark=courseworkMark;
this.examMark=examMark;
}
public void getResult(){ //Method to calculate student grade and result based on overall mark
courseworkMark=Input.getDouble("Enter Coursework Mark: ");
examMark=Input.getDouble("Enter Exam Mark: ");
totalMark=(double)(courseworkMark+examMark);
overallMark=(double)(totalMark/2);

if (overallMark >= 39.5 || courseworkMark >= 30 && examMark >= 30){
result = "PA";
} else {
result="Fail";
}
if (overallMark >=69.5){
grade="A";
} if (overallMark >= 59.5 && overallMark < 69.5){
grade="B1";
} if (overallMark >= 49.5 && overallMark < 59.5){
grade = "B2";
} if (overallMark >= 39.5 && overallMark < 49.5){
grade = "C";
} if (overallMark >= 29.5 && overallMark < 39.5){
grade = "D";
result="RB2";
} if (overallMark < 29.5){
grade = "E";
} if (examMark < 40 && courseworkMark < 40){
result = "RB2";
} if (examMark < 40 && courseworkMark >= 40){
result = "RC2";
} if (courseworkMark < 40 && examMark >= 40){
result = "RE2";
}
}
public String toString(){

String displayDetails = new String();
displayDetails+=String.format("%25s%25s%25s%25s%25s",courseworkMark, examMark, overallMark,grade,result);
return displayDetails;

}

}
////// Student Class
public class Student {

String studentName;
String bannerID;
int MAX_NUM_OF_STUDENTS;

Result result;


public Student(String studentName, String bannerID){
this.studentName=studentName;
this.bannerID=bannerID;
}
public void getResult(){
for(int count=0;count<MAX_NUM_OF_STUDENTS;count++){
result.getResult();
}
}


public String toString(){
String displayDetails = new String();
displayDetails+=this.result.toString();
displayDetails+=String.format("%-25s%25s",this.studentName, this.bannerID);

return displayDetails;

}
}
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Anderson wrote:how do i get my Student objects to each have their own result?


Create a member variable in class Student of type Result, and assign the result object that you create to that member variable.

Alan Anderson wrote:(please understand this time


The better you explain it, the easier you make it for us to understand, and the quicker you'll get useful answers.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Anderson wrote:
what i want to know is,after i create the Result object in the Student class,how do i get my Student objects to each have their own result? (please understand this time



The question is flawed.

You don't "create (one) Result object in the Student class." Rather, you declare a Result member variable in the Student class, and you initialize it at the appropriate time--when you have the relevant data--either at declaration or in the Student constructor or as a result of some method call.

For example, if I have a Person class with a Date field for birthday, here are examples of all of the above.

 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your effort mate,but can you have a look at my last post? i already have a member variable result declared in the Student class...what i need to do now is give every Student object i create its own "result"
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Anderson wrote:what i need to do now is give every Student object i create its own "result"



My example shows how to do that.

 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help...Am greatful
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.

Is it clear now, or are you still confused? If you're still having problems, post whatever code you've tried, and provide as much detail about what you're really trying to accomplish and what you don't understand.

We like to help, but sometimes it's not clear where the disconnect is.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you bet
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey am back...this time am sending the entire code...please have a look see and tell what am doing wrong.Tried your suggestion and it still didn't work. 3 classes....please note: the Student class is supposed to have the result (as a Result object) of the students on the module,

public class Student {

String studentName;
String bannerID;
double courseworkMark;
double examMark;
double totalScore;
Result result;


public Student(String studentName, String bannerID,double courseworkMark, double examMark){
this.studentName=studentName;
this.bannerID=bannerID;
this.courseworkMark=courseworkMark;
this.examMark=examMark;

}

public String getGrade(){
return result.getGrade();
}
public String getResult(){

return result.getResult();
}
public double getTotalScore(){
return result.getTotalScore();
}


public String toString(){
String studentDetails = new String();
studentDetails+=String.format("%-20s%20s%20s%20s%20s%20s%20s\n", this.studentName, this.bannerID,this.courseworkMark,this.examMark,this.totalScore,this.getGrade(),this.getResult());
return r;
}
}
//Result class
public class Result {
private double courseworkMark;
private double examMark;
private double totalMark;
private double overallMark;
String grade;
String moduleResult;

public Result(double courseworkMark, double examMark){
this.courseworkMark=courseworkMark;
this.examMark=examMark;
}
public String getGrade(){
this.overallMark=this.courseworkMark+this.examMark;
totalMark=this.overallMark/2;

if (totalMark >= 69.5){
grade="A";
if(totalMark>= 59.5 && overallMark < 69.5)
grade="B1";
if (totalMark >= 49.5 && totalMark < 59.5)
grade="B2";
if (totalMark >=40 && totalMark < 49.5)
grade="C";
if (totalMark >=29.5 && totalMark < 40)
grade = "D";
} else {
grade ="E";
}
return grade;
}


public String getResult(){
overallMark=courseworkMark+examMark;
totalMark=overallMark/2;

if(totalMark>=39.5 || courseworkMark >= 30 && examMark >= 30){
moduleResult= "PASS";
if(courseworkMark < 40 && examMark <40)
moduleResult ="RB2";
if (examMark < 40 && courseworkMark > 40)
moduleResult = "RC2";
if (courseworkMark < 40 && examMark > 40)
moduleResult = "RE2";
}
return moduleResult;
}

public double getTotalScore(){
return totalMark;
}
public double getCourseworkMark(){
return courseworkMark;
}
public double getExamMark(){
return examMark;
}

}
//Module class
public class Module {

Student [] students; //Array of Student objects

//Module Details
String moduleName;
double courseworkContribution= 0.6F; //coursework contributes 60% of overallMark
double examContribution=0.4F; //exam contributes 40% of overallMark
int MAX_NUM_OF_STUDENTS;
double courseworkAverage;
double examAverage;
double overallMarkAverage;

public Module(int MAX_NUM_OF_STUDENTS){ //constructor for creating objects of type Module
this.students = new Student[MAX_NUM_OF_STUDENTS];
}
public void addStudent(String studentName, String bannerID, double courseworkMark,double examMark){
this.students[this.MAX_NUM_OF_STUDENTS] = new Student(studentName, bannerID, courseworkMark,examMark);
}
public String toString(){
String studentDetails = new String();
studentDetails+=String.format("%-20s%20s%20s%20s%20s%20s%20s", "STUDENT NAME","BANNER ID", "CWRK(60)", "EXAM (40)", "TOTAL", "GRADE", "RESULT")+"\n";
for (int count=0; count<MAX_NUM_OF_STUDENTS; count++){
this.students[count].getGrade();
this.students[count].getResult();
this.students[count].getTotalScore();
studentDetails+=this.students[count].toString()+"\n";

}
return studentDetails;
}
}
//Test class
public class Test {
public static void main(String[]args) {

final int MAX_NUM_OF_STUDENTS=10;


Module md = new Module(MAX_NUM_OF_STUDENTS);

md.addStudent("Anderson Alan", "b0024545", 80,70);
md.addStudent("Brown Bill", "b94939939", 60,40);
md.addStudent("Carew Catherine", "B04993993", 59,69);

System.out.println(md);


}
}
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Anderson wrote:Hey am back...this time am sending the entire code.



Please UseCodeTags(⇐click) if you want people to read it. You can edit your post to add them.

..please have a look see and tell what am doing wrong.



What specific problem are you having?

Tried your suggestion and it still didn't work.



Then you misunderstood it, or didn't do it correctly. Hard to say without more detail as to what the actual problem is. Note that ItDoesntWorkIsUseless(⇐click).
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's still very hard to read, as there's no indentation. I hope it doesn't look like that in your editor. Please copy/paste from your editor, so that indentation will be preserved. And please edit your existing post, rather than creating a new one.

Also, I still have no idea what difficulty you're having or what's going wrong.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks man,am not sure how to explain this anymore.i'll just try and figure it out.Thanks
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Anderson wrote:please note: the Student class is supposed to have the result (as a Result object) of the students on the module,


Your Student class already has the result, in the form of a Result object. Look at line 9 of your code, you have a member variable named 'result' there, and it's a Result. You also have a getter for it (lines 22-25). The only thing that's missing in the Student class is that you're not initializing the member variable 'result' anywhere, so it will always be null. You'll need to add something to class Student to set the result in a Student object.

Like Winston, I don't understand exactly what you're after, so if that's not what you mean, then please explain it clearly.
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah,that's exactly where am lost.i need to be able to initialize the member variable 'result' for every Student object i create. Note i am already creating the student objects in the Module class via the method:

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add a parameter to the constructor of class Student (line 9), of type Result, and use that to set the value of the member variable 'result' (exactly the same as you're doing with the other member variables of class Student).

You'll also need to change lines 115 and 116 to pass the Result object to the constructor of Student, and lines 140 - 142.

Strange, that addStudent() method you posted above looks different than what you posted earlier. In your earlier code it had 4 parameters instead of 2.
 
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the final solution,
this thread seems confusing
 
Ojonugwa Ochalifu
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,life hasn't been better since we last 'spoke'...i'll just post a rough specification of the classes in my program.

Module:
Has an array of Student objects as well as module details...
Student:
has the name and banner ID of the student and their result on the module(as a Result object)
Result:
has the coursework mark,exam mark, overall mark grade(string e.g "A" or "B" and result (string e.g "Pass" or "Fail")

In what direction do i steer this lost ship?
 
knowledge is the difference between drudgery and strategic action -- tiny ad
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic