Question

Java Program 1. Write a class that reads in a group of test scores (positive integers...

Java Program

1. Write a class that reads in a group of test scores (positive integers from 1 to 100) from the keyboard. The main method of the class calls a few other methods to calculate the average of all the scores as well as the highest and lowest score. The number of scores is undetermined but there will be no more than 50. A negative value is used to end the input loop.

import java.util.Scanner;
public class GradeStat
{
final static int MAXGRADES = 50; // maximum # of grades
public static void main (String[] args) {
int[] gradeArray = new int[MAXGRADES];
int pos = 0; // Index to the array

double avgOfGrades; // contains the average of grades
int highestGrade; // contains the highest grade
int lowestGrade; // contains the lowest grade
int sizeOfArray; // the size of the used array

// input grades Scanner scan = new Scanner ( System.in );
System.out.println("Please input a grade from 1 to 100, a negative number to exit");
gradeArray[pos] = scan.nextInt();

while (gradeArray[pos] >=1) {
pos++; // increment subscript
System.out.println("Please input a grade from 1 to 100");
gradeArray[pos] = scan.nextInt();
}

sizeOfArray = pos + 1; // The actual number of elements used in the array

// call the Average method to calculate the average of gradeArray
avgOfGrades = ________________________________________;

System.out.println("The average of all the grades is “+ avgOfGrades);

// call the Highest method to calculate the highest score of gradeArray
highestGrade = ________________________________________;
System.out.println("The highest grade is " + highestGrade);

// call the Lowest method to calculate the lowest score of gradeArray
lowestGrade = _________________________________________;
System.out.println("The lowest grade is " + lowestGrade);
}

// Fill in the code to determine average grade
public static double Average(int [] array, int size)
{
}

// Fill in the code to determine highest grade
public static int Highest (int [] array, int size)
{
}

// Fill in the code to determine lowest grade
public static int Lowest (int [] array, int size)
{
}
}

2. Complete the following program that defines class Square. It has one data field called side which keeps the length of the side of a square. The class has the following methods: 1) setSide(double si), a void method that receives “si” as a parameter and sets this value to side; 2) getArea(), a method that returns the area of a square. 3) a default constructor method that sets data member side to 0; 3) a constructor that takes the “si” as a parameter and sets this value to data member side.

import java.util.Scanner;
public class Square
{
//define the data member here

// implement all the methods here

public static void main(String[] args)

{

double s;

// create a keyboard object Scanner kbd=________________________________;
System.println(“Please input the side of a square”);
// read in the value for rad from keyboard
s =_______________________________________;
// create an object of Square using the default constructor
Square aSquare = _______________________;
// create an object of Square with 2 as its side value
Square bSquare = _______________________;
__________________________________; // display areae of aSquare
__________________________________; // Sets the side of aSquare to the value of s
__________________________________; // display areae of aSquare

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1:

import java.util.Scanner;

public class GradeStat {
   final static int MAXGRADES = 50; // maximum # of grades

   public static void main(String[] args) {
       int[] gradeArray = new int[MAXGRADES];
       int pos = 0; // Index to the array
       Scanner scan = new Scanner(System.in);
       double avgOfGrades; // contains the average of grades
       int highestGrade; // contains the highest grade
       int lowestGrade; // contains the lowest grade
       int sizeOfArray; // the size of the used array

       // input grades Scanner scan = new Scanner ( System.in );
       System.out.println("Please input a grade from 1 to 100, a negative number to exit");
       gradeArray[pos] = scan.nextInt();

       while (gradeArray[pos] >= 1) {
           pos++; // increment subscript
           System.out.println("Please input a grade from 1 to 100");
           gradeArray[pos] = scan.nextInt();
       }

       sizeOfArray = pos + 1; // The actual number of elements used in the
                               // array

       // call the Average method to calculate the average of gradeArray
       avgOfGrades = Average(gradeArray, pos);

       System.out.println("The average of all the grades is " + avgOfGrades);

       // call the Highest method to calculate the highest score of gradeArray
       highestGrade = Highest(gradeArray, pos);
       System.out.println("The highest grade is " + highestGrade);

       // call the Lowest method to calculate the lowest score of gradeArray
       lowestGrade = Lowest(gradeArray, pos);
       System.out.println("The lowest grade is " + lowestGrade);
   }

   // Fill in the code to determine average grade
   public static double Average(int[] array, int size) {
       double sum = 0;
       for (int i = 0; i < size; i++)
           sum += array[i];
       return sum / size;
   }

   // Fill in the code to determine highest grade
   public static int Highest(int[] array, int size) {
       int max = array[0];
       for (int i = 1; i < size; i++)
           if (array[i] > max)
               max = array[i];
       return max;
   }

   // Fill in the code to determine lowest grade
   public static int Lowest(int[] array, int size) {
       int min = array[0];
       for (int i = 1; i < size; i++)
           if (array[i] < min)
               min = array[i];
       return min;
   }
}

Answer 2:

import java.util.Scanner;

public class Square {
   private double side;

   public Square() {
       side = 0;
   }

   public Square(double s) {
       side = s;
   }

   public double getSide() {
       return side;
   }

   private void setSide(double aS) {
       side = aS;
   }

   private double getArea() {
       return side * side;
   }

   // define the data member here

   // implement all the methods here

   public static void main(String[] args)

   {

       double s;
       Scanner sc = new Scanner(System.in);
       // create a keyboard object Scanner
       System.out.println("Please input the side of a square");
       // read in the value for rad from keyboard
       s = sc.nextDouble();
       // create an object of Square using the default constructor
       Square aSquare = new Square();
       // create an object of Square with 2 as its side value
       Square bSquare = new Square(2);
       System.out.println("Area : " + aSquare.getArea());
       aSquare.setSide(s);
       System.out.println("Area : " + aSquare.getArea());

   }

}

Add a comment
Know the answer?
Add Answer to:
Java Program 1. Write a class that reads in a group of test scores (positive integers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // This program will read in a group of test scores (positive integers from 1 to...

    // This program will read in a group of test scores (positive integers from 1 to 100) // from the keyboard and then calculate and output the average score // as well as the highest and lowest score. There will be a maximum of 100 scores. // #include <iostream> using namespace std; float findAverage (const int[], int); // finds average of all grades int findHighest (const int[], int); // finds highest of all grades int findLowest (const int[], int); //...

  • Write a JAVA program that prompts the user for student grades and displays the highest and...

    Write a JAVA program that prompts the user for student grades and displays the highest and lowest grades in the class. The user should enter a character to stop providing values. Starter code: import java.util.Scanner; public class MaxMinGrades{   public static void main(String[] args){     Scanner input = new Scanner(System.in);     System.out.println("Enter as many student grades as you like. Enter a character to stop.");     double grade = input.nextDouble();     double minGrade = Double.MAX_VALUE;     double maxGrade = Double.MIN_VALUE;     while (Character.isDigit(grade)) {       if (grade == 0)...

  • Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public...

    Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String input =""; System.out.println("Welcome to the easy square program"); while(true) { System.out.println("Enter the length of the side of a square or enter QUIT to quit"); try { input = keyboard.nextLine(); if(input.equalsIgnoreCase("quit")) break; int length = Integer.parseInt(input); Square s = new Square(); s.setLength(length); s.draw(); System.out.println("The area is "+s.getArea()); System.out.println("The perimeter is "+s.getPerimeter()); } catch(DimensionException e)...

  • In Java 8 Write a HomeworkGrades class that stores the homework grades for 8 chapters into...

    In Java 8 Write a HomeworkGrades class that stores the homework grades for 8 chapters into an array of doubles. • Write a constructor that takes an array as input and copies the contents of the array into the class’ array. • Write methods to calculate: • The average of the homework grades • The lowest grade • Write a main function that creates an array with this data: • double[ ] grades = {98.7, 77.9, 90, 83, 67, 33,...

  • Java Question Method and 1 dimension arrays (20) YouTube ㄨ Electronics, Cars, Fashion, Co × Upload...

    Java Question Method and 1 dimension arrays (20) YouTube ㄨ Electronics, Cars, Fashion, Co × Upload Assignment: Program xy 9 Program3.pdf eduardo C Secure https://blackboard.kean.edu/bbcswebdav/pid-736390-dt-content-rid-2804166 1/courses/18SP CPS 2231 06/Program3.pdf Concepts: Methods and one dimensional Arrays Point value: 45 points Write a Java Class that reads students grades and assigns grades based on the following grading "curve .Grade is A if the score is greater than or equal to the highest score - 10 .Grade is B if the score is...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Create a class called Student. This class will hold the first name, last name, and test...

    Create a class called Student. This class will hold the first name, last name, and test grades for a student. Use separate files to create the class (.h and .cpp) Private Variables Two strings for the first and last name A float pointer to hold the starting address for an array of grades An integer for the number of grades Constructor This is to be a default constructor It takes as input the first and last name, and the number...

  • Below is a java program that inputs an integer grade and turns it into a letter...

    Below is a java program that inputs an integer grade and turns it into a letter grade. Update the below java code as follows and comment each line to explain what is happening: 1. Convert the if-else-if code block to a switch statement to solve the problem. 2. Use modulus to convert the grade input so that the range of grades are converted to one value. (comment the line) import java.util.Scanner; public class GradeLetterTest { public static void main(String[] args)...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT