Java please import java.util.Random; public class Student { //Instances variable
ID: 3694065 • Letter: J
Question
Java please
import java.util.Random;
public class Student {
//Instances variables
private String name;
//Default constructor
public Student()
{
}
//Constructor
public Student(String name, String town, double score)
{
}
//Accessor and mutator
public setScore( )
{
}
public setName( )
{
}
public getName()
{
}
public getScore()
{
}
}
import java.util.ArrayList;
public class StudentTester
{
public static void main(String[] args)
{
//Create the array list
ArrayList< > studentArrayList = ;
//Create three students
Student S1 = new Student("Ali","Aljoubil",65);
//Add students to the array list
studentArrayList.add(S1);
System.out.println("The name of students who have an odd score: ");
//Find and print the name(s) of student(s) who have odd score
for ( )
{
if( )
System.out.println( );
}
double tot=0;
// Compute and print the average score
for ( )
{
tot=tot+ ;
}
System.out.println("avrage is " + );
double max = studentArrayList.get(0).getScore();
//Find and print the highest score
for ( )
{
if( )
max = ;
}
System.out.println("max is " + max);
}
}
Explanation / Answer
Student.java
package org.students;
import java.util.Random;
public class Student {
//Declaring variables
private String name;
private String town;
private int score;
//Declaring Constant
private static final String university_name = "SEU";
//Default Constructor
public Student() {
Random r = new Random();
this.name = "NEW";
this.town = "KSA";
this.score = r.nextInt((100 + 1));
}
//Parameterized Constructor
public Student(String name, String town, int score) {
super();
this.name = name;
this.town = town;
this.score = score;
}
//Setters and Getters
public String getName() {
return name;
}
public void setName(String name) {
try {
if (name.length() > 3) {
this.name = name;
}
} catch (Exception e) {
System.out.println(":: Name must be greater than 3 charecters ::");
}
}
public int getScore() {
return score;
}
public void setScore(int score) {
try {
if (score > 100) {
this.score = score;
}
} catch (Exception e) {
System.out.println(":: score must be greater than 100 ::");
}
}
}
________________________________________________________________________________________
StudentTester.java
package org.students;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
public class StudentTester {
public static void main(String[] args) {
//Creating an ArrayList Object
ArrayList<Student> studentArrayList = new ArrayList();
//Creating the students Objects by passing values as arguments.
Student st1 = new Student("williams", "RSV", 80);
Student st2 = new Student("Tony", "MKS", 90);
Student st3 = new Student("Greg", "CVR", 91);
//Add Student Objects to the ArrayList.
studentArrayList.add(st1);
studentArrayList.add(st2);
studentArrayList.add(st3);
// Find and print the name(s) of student(s) who have odd score
System.out.println("The name of students who have an odd score: ");
for (Student st : studentArrayList) {
if (st.getScore() % 2 != 0)
System.out.println("'" + st.getName()
+ "' has got the odd Score " + st.getScore());
}
System.out.println(" ");
// Compute and print the average score
double tot = 0;
for (Student st : studentArrayList) {
tot = tot + st.getScore();
}
System.out.println("Average of students scores is ::" + tot
/ studentArrayList.size());
System.out.println(" ");
// Find and print the highest score
double max = studentArrayList.get(0).getScore();
for (Student st : studentArrayList) {
if (max < st.getScore())
max = st.getScore();
}
System.out.println("The Maximum score is :: " + max);
}
}
_________________________________________________________________________________________
output:
The name of students who have an odd score:
'Greg' has got the odd Score 91
Average of students scores is ::87.0
The Maximum score is :: 91.0
__________________________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.