. IST 240- L03 Lab: Java – Complex Classe s What will be assessed Your knowledge
ID: 3872948 • Letter: #
Question
. IST 240- L03 Lab: Java – Complex Classes
What will be assessed
Your knowledge of how to work with classes within classes
Your knowledge of how to use the DOT operation/notation
Deliverables
app.java, student.java and group.java as requested below.
Contents
Create an application that:
Creates 4 students
Build upon the solutions of the previous labs (e.g., using app.java and student.java)
You will need a version of student.java that has a working whatsup() method
Creates a groupFirst, you have to design the new Java class called group
A group has a name
A group has 4 students
Create two methods in group.java
a method that displays the group name and the name of each student in the group
a method that displays the participation in a group. Participation is the sum of all the group members' (students') participation. Participation is an int number that varies from 1 to 10 (1 meaning very little participation, 10 meaning great participation)
participation starts in student using a random number (in a similar fashion of whatsUp()) to return the student's participation. For instance, when the method is called, we may have: student member #1, John, with a participation of 3; student #2, Mary, with a participation of 10; and student #3, Fred, with a participation of 5, which will give a participation of (3 + 10 + 5) = 18.
in app.java
using the group variable (instance, object) in group.java:
display the group name, and total group participation
display information about what the third student in the group (retrieving information from the student class variables in the group, not from student variables in the app) is up to now (use the whatsUp() method).
For instance: “John” is “doing Java”
Pay attention, you have to use a the group variable (instance, object) in app.java
Something like System.out.println("the student is " + g1.______________);
Suggestions:
Start by creating a class called group with only one attribute, GROUP_NAME
Make it work, using the app to display only the group name
Then you think how to add the students to the group (this is tricky, but that’s what you have to learn with this lab)
See the examples in the lesson: video (Links to an external site.)Links to an external site.
For calculating participation:
You will need a new method called participation in student.java
Start by copying the whatsUp() method in student.java to create a participation method in student.java.
Make the necessary adjustments for it to return a number (it can be an int number from 1 to 10)
Then work on a participation method on group.java gathering participation from each student in the group
In the app.java create students and create a group (from previous labs and assignments), and then display the group participation.
Explanation / Answer
Program Code:
Part I:
//student.java
public class student
{
//local variables
String name;
//Constructor
public student(String studName)
{
name=studName;
}
//getters and setters
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
//to use the ArrayList class
import java.util.*;
public class group
{
//local variables
String groupName;
ArrayList<student> studName=new ArrayList<student>();
//default consturtor
public group()
{
}
//setters and getters
public String getGroupName()
{
return groupName;
}
public void setGroupName(String groupName)
{
this.groupName = groupName;
}
public ArrayList<student> getstudName()
{
return studName;
}
public void setstudName(String []names)
{
for(int i=0;i<4;i++)
{
student s=new student(names[i]);
studName.add(s);
}
}
//dispalyStudent method that returns the specified student names
public String displayStudent(int index)
{
return studName.get(index).getName();
}
//toString method that returns the overall information of
//the group and names of the students of the respective
//group
public String toString()
{
String str=getGroupName();
for(int i=0;i<studName.size();i++)
str+=" Student #"+(i+1)+": "+studName.get(i).getName();
str+=" ";
return str;
}
}
//app.java
public class app
{
//main method
public static void main(String args[])
{
//declare a string array that contains the names of the students
String studNames[]={"John", "Kevin", "Albert","Leandro"};
//define the object of the class group
group g=new group();
g.setGroupName("Java");
g.setstudName(studNames);
//display the group name
System.out.println("Group Name: "+g.getGroupName());
//display the list of the students by passing the index values
System.out.println("The list of students in the group is: ");
for(int i=0;i<studNames.length;i++)
System.out.println("Student "+(i+1)+": "+g.displayStudent(i));
//display the third student information
System.out.println(g.displayStudent(2)+" is doing "+ g.getGroupName());
}
}
Sample Output:
Group Name: Java
The list of students in the group is:
Student 1: John
Student 2: Kevin
Student 3: Albert
Student 4: Leandro
Albert is doing Java
--------------------------------------------------------------------------------------------------------------------------------------
Part II:
//student.java
public class student
{
//local variables
String name;
int participation;
//Constructor
public student(String studName)
{
name=studName;
participation=(int)(Math.random()*10)+1;
}
//getters and setters
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getparticipation()
{
return participation;
}
}
//to use the ArrayList class
import java.util.*;
public class group
{
//local variables
String groupName;
ArrayList<student> studName=new ArrayList<student>();
//default consturtor
public group()
{
}
//setters and getters
public String getGroupName()
{
return groupName;
}
public void setGroupName(String groupName)
{
this.groupName = groupName;
}
public ArrayList<student> getstudName()
{
return studName;
}
public void setstudName(String []names)
{
for(int i=0;i<4;i++)
{
student s=new student(names[i]);
studName.add(s);
}
}
//dispalyStudent method that returns the specified student names
public String displayStudent(int index)
{
return studName.get(index).getName();
}
//totalParticipation method computes the overall participation of
//the students in the group
public int totalParticipation()
{
int total=0;
for(int i=0;i<studName.size();i++)
total+=studName.get(i).getparticipation();
return total;
}
//toString method that returns the overall information of the
//group and students of the respective group
public String toString()
{
String str=getGroupName();
for(int i=0;i<studName.size();i++)
str+=" Student #"+(i+1)+": "+studName.get(i).getName() +" with participation: "+studName.get(i).getparticipation();
str+=" ";
return str;
}
}
//app.java
public class app
{
//main method
public static void main(String args[])
{
//declare a string array that contains the names of the students
String studNames[]={"John", "Kevin", "Albert","Leandro"};
//define the object of the class group
group g=new group();
g.setGroupName("Java");
g.setstudName(studNames);
//display the group name
System.out.println("Group Name: "+g.getGroupName());
//display the list of the students by passing the index values
System.out.println("The list of students in the group is: ");
for(int i=0;i<studNames.length;i++)
System.out.println("Student "+(i+1)+": "+g.displayStudent(i));
//display the third student information
System.out.println(g.displayStudent(2)+" is doing "+ g.getGroupName());
//display the each student in the group with their participation
System.out.println("The list of students with participation rate is: ");
System.out.println(g.toString());
//display the overall participation rate of the group
System.out.println("The overall total participation is the group "+g.getGroupName()+" is: "+g.totalParticipation());
}
}
Sample Output:
Group Name: Java
The list of students in the group is:
Student 1: John
Student 2: Kevin
Student 3: Albert
Student 4: Leandro
Albert is doing Java
The list of students with participation rate is:
Java
Student #1: John with participation: 3
Student #2: Kevin with participation: 5
Student #3: Albert with participation: 4
Student #4: Leandro with participation: 9
The overall total participation is the group Java is: 21
THANK YOU
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.