Question

In: Computer Science

writing a well documented program with multiple classes and arrays.   Include a pledge and the IDE...

writing a well documented program with multiple classes and arrays.  

Include a pledge and the IDE used in the descriptive header of the driver class

  1. Create a Student class with
    1. Private instance variables for first and last name, three integer scores, the average ,and letter grade
    2. A constructor with values
    3. Methods to set and get the values of the instance variables
    4. Methods to get the average and letter grade
    5. Method to calculate the average of the three scores and to assign this value to the instance variable average
    6. Method to determine the letter grade and to assign this value to the instance variable grade
    7. Method to format and print the name, scores, average ( to two decimal places), and letter grade. (using printf() in a toString method)
  2. Create a StudentMod3 class
    1. Declare an array of objects of type Student for three people
    2. One person at a time, read their first and last name, and three scores, into each of the three elements of the array
    3. Determine the average and letter grade of all the three students. Letter grade is based on the following criteria - average 90-100 grade is A, 80-89.99 grade is B, 70-79.99 grade is C, all others F (use a nested if)
    4. Output the name, scores, average, and letter grade of the three people

Solutions

Expert Solution

The following is the code for the above question. It has met all the requirements stated in the question.

import java.util.Scanner;
public class Main
{
        public static void main(String[] args) {
            Scanner in=new Scanner(System.in);
                Student[] students=new Student[3]; //declare array of Student objects
                int i;
                String fname,lname;
        int s1,s2,s3;
        double average;
        char grade;
                for(i=0;i<3;i++)
                {                      //input all the values for 3 students
                    fname=in.nextLine();
                    lname=in.nextLine();
                    s1=in.nextInt();
                    s2=in.nextInt();
                    s3=in.nextInt();
                    in.nextLine();  //this is used when int is captured and next line to be captured to overcome errors
                    students[i]=new Student(fname,lname,s1,s2,s3); //store them in Student array
                }
                for(i=0;i<3;i++)
                {
                    students[i].cal_average(); //calculate average
                    students[i].cal_grade();  //calculate grade
                    students[i].tostring();  //call tostring() method (Note: toString() should return String, but in question it is asked to printf(). Hence here we cant override toString() otherwise it will raise error. Try once for your understanding Thats why tostring() is used here)
                    System.out.println();
                }
        }
}
class Student
{
    private String fname,lname;
    private int s1,s2,s3;
    private double average;
    private char grade;
    Student(String fname,String lname,int s1,int s2,int s3) //constrcutor to initialize values. You can also use set methods
    {
        this.fname=fname;
        this.lname=lname;
        this.s1=s1;
        this.s2=s2;
        this.s3=s3;
    }
    //set and get methods for all instance variables
    public void setfname(String fname)
    {
        this.fname=fname;
    }
    public String getfname()
    {
        return this.fname;
    }
    public void setlname(String lname)
    {
        this.lname=lname;
    }
    public String getlname()
    {
        return this.lname;
    }
    public void sets1(int s1)
    {
        this.s1=s1;
    }
    public int gets1()
    {
        return this.s1;
    }
    public void sets2(int s2)
    {
        this.s2=s2;
    }
    public int gets2()
    {
        return this.s2;
    }
    public void sets3(int s3)
    {
        this.s3=s3;
    }
    public int gets3()
    {
        return this.s3;
    }
    public double getaverage()
    {
        return this.average;
    }
    public char getgrade()
    {
        return this.grade;
    }
    public void cal_average() //calculate average
    {
        this.average=(double)((double)(this.s1+this.s2+this.s3)/(double)(3)); //type conversion
    }
    public void cal_grade() //calculate grade
    {
        double avg=this.average;
        char grd;
        if(avg>=90 && avg<=100)
        {
            grd='A';
        }
        else if(avg>=80 && avg<90)
        {
            grd='B';
        }
        else if(avg>=70 && avg<80)
        {
            grd='C';
        }
        else
        {
            grd='F';
        }
        this.grade=grd;
    }
    public void tostring()  //tostring() print the information
    { 
        System.out.printf("Fname:%s, lname:%s, Score 1:%d, Score 2:%d, Score 3:%d, average: %.2f, grade: %c",getfname(),getlname(),gets1(),gets2(),gets3(),getaverage(),getgrade());
    } 
}

I am also attaching the output and code screenshot for your reference.

Output and code screenshot:

#Please don't forget to upvote if you find the solution helpful. Feel free to ask doubts if any, in the comments section. Thank you.


Related Solutions

This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files...
This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files into a new C++ project.    NOTE: if your IDE does not allow you to create projects - or you are not sure how to do it - then you may combine the two cpp files into a single file. 2) Complete the program by adding code the the class methods. You may want to study the existing code first to get a feel...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
Writing an nth root program in C++ using only: #include using namespace std; The program must...
Writing an nth root program in C++ using only: #include using namespace std; The program must be very basic. Please don't use math sqrt or pow . For example, the 4th root of 16 is 2 because 2 * 2 * 2 * 2 = 16. The 4th root of 20 is 2 because 2 * 2 * 2 * 2 = 16 and 16 is less than 20, and 3 * 3 * 3 * 3 = 81, which...
Write a program which shows how two-dimensional arrays which contain multiple copies of your id number...
Write a program which shows how two-dimensional arrays which contain multiple copies of your id number and age are passed to methods. Explain in your own words each method and class used in the program. My id number is 12133149
Write a program which shows how two-dimensional arrays which contain multiple copies of your id number...
Write a program which shows how two-dimensional arrays which contain multiple copies of your id number and age are passed to methods. Explain in your own words each method and class used in the program.
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source...
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source code) a program (name it LargestOccurenceCount) that read from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the loop). The program should ignore any negative input and should continue to read user inputs until 0 is entered. The program should display the largest value and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT