Question

In: Computer Science

Code Using Arrays in Java There is a new ruling for the only elevator located at...

Code Using Arrays in Java

There is a new ruling for the only elevator located at Block B. Students who need to ride the elevator, must line up in a queue. The rated load in pounds for the elevator is based on the inside net platform areas. The maximum load for the elevator is 500 pound.

Write a program to read the students weight (in pound) in the line and calculate the number of students that are allowed to enter the elevator before it makes a loud noise.

Input

The first line of input is T (1 ≤ T ≤ 100) which is the number of test case. This is followed by T lines of input. Each line starts with X (X ≤ 100) which is the number of students who want to ride the elevator. This is then followed by a list of X data which is the students’ weight in the line.

Output

For each test case, the output contains a line in the format "Case #x: y", where x is the case number (starting from 1) and y indicates the number of students in the line that are allowed to ride the elevator.

Sample Input

3
9 45 25 50 46 10 55 50 83 68
5 66 155 93 101 90 
8 64 70 50 45 85 74 110 95 

Sample Output

Case #1: 9
Case #2: 4
Case #3: 7


Solutions

Expert Solution

Code below:

import java.util.Scanner;

public class Elevator {

    private static final int MAX_LOAD_ALLOWED = 500;

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // take number of test cases as input.
        int t = scanner.nextInt();

        int[] numberOfStudentsAllowed = new int[t];

        for (int caseNumb = 0; caseNumb < t; ++caseNumb) {

            // take inputs per test case.
            int n = scanner.nextInt();
            int[] studentsWeightsArray = new int[n];

            // defaulting students allowed to value n for every case Numb.
            numberOfStudentsAllowed[caseNumb] = n;

            for (int i = 0; i < n; ++i) {
                studentsWeightsArray[i] = scanner.nextInt();
            }

            // find the total number of students that will be allowed.
            int tempSum = 0;
            for (int i = 0; i < n; ++i) {

                tempSum += studentsWeightsArray[i];
                if (tempSum > MAX_LOAD_ALLOWED) {
                    numberOfStudentsAllowed[caseNumb] = i;
                }
            }

        }

        // Print the outputs.
        for (int i = 0; i < t; ++i) {

            System.out.println("Case #" + (i + 1) + ": " + numberOfStudentsAllowed[i]);

        }
    }

}

Sample input output

Sample code for indentation


Related Solutions

JAVA I need to write a code that calculates mean and standard deviation using arrays and...
JAVA I need to write a code that calculates mean and standard deviation using arrays and OOP. This is what I have so far. I'm close but my deviation calculator method is throwing errors. Thanks so much :) import java.util.Scanner; import java.util.Arrays; public class STDMeanArray { private double[] tenUserNums = new double[10];//Initialize an array with ten index' private double mean; private double deviation; Scanner input = new Scanner(System.in);//create new scanner object /*//constructor public STDMeanArray(double tenUserNum [], double mean, double deviation){...
Using Java, write the the following code. Only the bold needs to be answered Here is...
Using Java, write the the following code. Only the bold needs to be answered Here is the driver: package assign public class A3Driver { public static void main(String[] args) { //Test GSUStudent object GSUStudentWorker gsw = new GSUStudentWorker("Bunny","Bugs", 2000, 9, 8, 9001234); gsw.setGPA(3.7); gsw.setHourlyRate(10.00); gsw.setTotalHours(30); System.out.println(gsw); GSUStudentWorker gsw2 = new GSUStudentWorker("Bunny","Betty", 1999, 9, 8, 9002413); gsw2.setGPA(4.0); gsw2.setHourlyRate(10.00); gsw2.setTotalHours(45); System.out.println(gsw2); //Test GSUStoreSupervisor GSUStoreSupervisor gss = new GSUStoreSupervisor("Duck","Daffy", 1980, 9, 8); gss.setMonthlyPay(4000); System.out.println(gss); //test GSUSuperStudent GSUSuperStudent gsuper = new GSUSuperStudent("Mouse","Minny", 1990, 9,...
Using Java, write the the following code. Only the bold needs to be answered Here is...
Using Java, write the the following code. Only the bold needs to be answered Here is the driver: package assign public class A3Driver { public static void main(String[] args) { //Test GSUStudent object GSUStudentWorker gsw = new GSUStudentWorker("Bunny","Bugs", 2000, 9, 8, 9001234); gsw.setGPA(3.7); gsw.setHourlyRate(10.00); gsw.setTotalHours(30); System.out.println(gsw); GSUStudentWorker gsw2 = new GSUStudentWorker("Bunny","Betty", 1999, 9, 8, 9002413); gsw2.setGPA(4.0); gsw2.setHourlyRate(10.00); gsw2.setTotalHours(45); System.out.println(gsw2); //Test GSUStoreSupervisor GSUStoreSupervisor gss = new GSUStoreSupervisor("Duck","Daffy", 1980, 9, 8); gss.setMonthlyPay(4000); System.out.println(gss); //test GSUSuperStudent GSUSuperStudent gsuper = new GSUSuperStudent("Mouse","Minny", 1990, 9,...
Using Java, write the the following code. Only the bold needs to be answered Here is...
Using Java, write the the following code. Only the bold needs to be answered Here is the driver: package assign public class A3Driver { public static void main(String[] args) { //Test GSUStudent object GSUStudentWorker gsw = new GSUStudentWorker("Bunny","Bugs", 2000, 9, 8, 9001234); gsw.setGPA(3.7); gsw.setHourlyRate(10.00); gsw.setTotalHours(30); System.out.println(gsw); GSUStudentWorker gsw2 = new GSUStudentWorker("Bunny","Betty", 1999, 9, 8, 9002413); gsw2.setGPA(4.0); gsw2.setHourlyRate(10.00); gsw2.setTotalHours(45); System.out.println(gsw2); //Test GSUStoreSupervisor GSUStoreSupervisor gss = new GSUStoreSupervisor("Duck","Daffy", 1980, 9, 8); gss.setMonthlyPay(4000); System.out.println(gss); //test GSUSuperStudent GSUSuperStudent gsuper = new GSUSuperStudent("Mouse","Minny", 1990, 9,...
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
For this question we will be using arrays and classes in Java to compute the min,...
For this question we will be using arrays and classes in Java to compute the min, max, and average value of items for a given array of integers. Complete the following using the base template provided below: -Create methods for min, max, and average and call them from main to print out their values. -Add a method to determine the median (http://www.mathsisfun.com/median.html) and print that value. This method is currently not in the template, so you will need to add...
java code: Problem 1: Create a class called Elevator that can be moved between floors in...
java code: Problem 1: Create a class called Elevator that can be moved between floors in an N-storey building. Elevator uses a constructor to initialize the number of floors (N) in the building when the object is instantiated. Elevator also has a default constructor that creates a five storey building. The Elevator class has a termination condition that requires the elevator to be moved to the main (i.e., first) floor when the object is cleaned up. Write a finalize() method...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an odd positive integer. The program reads a value (n) entered by the user and prints an n x n grid displaying a large letter X. The left half should be made up of pluses (+) and the right half should be made with the character "x" and the very center should be an asteric...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an integer between 1 and 20. If the user enters an illegal number, the program repeatedly asks the user to enter the correct one. If the user has not entered a correct number after 10 attempts, the program chooses the number 10 as the user's number. The program prints the cube of the user's number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT