2 Part Question: Part 1 Create an application that: 1) Creates 4 students (using
ID: 639234 • Letter: 2
Question
2 Part Question:
Part 1
Create an application that:
1) Creates 4 students (using app.java and student.java)
2) Creates a group
- First you have to design the new Java class called group
- A group has a name
- A group has 4 students
3) Create a method in group.java that displays the group name and the name of each student in the group
4) in app.java
- using a method in group.java
- from app.java, display information about what the third student in the group (retrieving information from the student in the group,not from student in the app) is up to now.
- (For instance:
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.