Concept explainers
Repeat Exercise 2 in Chapter 7, but use an instance of ArrayList instead of an array. Do not read the number of values, but continue to read values until the user enters a negative value.
Explanation of Solution
Program Plan:
- Import required package.
- Define “CountFamiles”class.
- Define main function.
- Create object for scanner class.
- Create array “incomeArr” by using “ArrayList”.
- Display the prompt statement.
- Assign “additionalValues” to “true”.
- Initializes required variable.
- Read input of income from user up to the value of “additionalValues” is “false”.
- Display prompt statement for each family.
- Read each family income from user and store it to “incomeValues”.
- If “incomeValues” is less than “0”, then set “additionalValues” to “false”.
- Otherwise, add the “incomeValues” to array “incomeArr”.
- Increment the index.
- Compute maximum income using “for” loop.
- If “incomeArr.get(i)” is greater than maximum income, then assign maximum income to “incomeArr.get(i)”.
- Display maximum income.
- Display “10” percent of maximum income by “0.1 * maximumIncome”.
- Initializes the count value to “0”.
- Compute the count of families with incomes less than “10” percentage of maximum income using “for” loop.
- Check condition. If value of “incomeArr.get(i)” is less than “0.1 * maximumIncome”, then increment count value and display the family income with less than “10” percentage of income by family wise.
- Finally display the count of families.
- Define main function.
Program:
The below java program is used to counts the number of families with less than “10” percent of maximum income using array.
“CountFamiles.java”
//Import required package
import java.util.*;
//Define "CountFamiles" class
public class CountFamiles
{
//Define main function
public static void main(String[] args)
{
//Create object for scanner class
Scanner input = new Scanner(System.in);
/* Create array "incomeArr" by using "ArrayList" */
ArrayList<Double> incomeArr = new ArrayList<Double>();
//Display the prompt statement
System.out.println("Enter the income for each family. Use a negative value to indicate the end ");
//Assign "additionalValues" to "true"
boolean additionalValues = true;
//Initializes required variable
int i = 0;
/* Read input of income from user upto the value of "additionalValues" is "false" */
while(additionalValues)
{
//Display prompt statement for each family
System.out.print("Enter Income for family " + (i +1) + ": ");
/* Read each family income from user and store it to "incomeValues" */
double incomeValues = input.nextDouble();
/* If "incomeValues" is less than "0", then */
if(incomeValues < 0)
/* Set "additionalValues" to "false" */
additionalValues = false;
/* Otherwise */
else
/* Add the "incomeValues" to array "incomeArr" */
incomeArr.add(incomeValues);
//Increment the index
i++;
}
//Compute maximum income using "for" loop
double maximumIncome = incomeArr.get(0);
for(i = 0; i < incomeArr.size(); i ++)
{
/* If incomeArr.get[i] is greater than maximum income, then */
if(incomeArr.get(i) > maximumIncome)
//Assign maximum income to "incomeArr.get(i)"
maximumIncome = incomeArr.get(i);
}
//Display maximum income
System.out.println("The maximum income is: " + maximumIncome);
//Display 10 percent of maximum income
System.out.println("10% of maximum income is: " + (0.1 * maximumIncome));
//Display prompt statement
System.out.println("\nDisplaying all families with incomes less than 10% of the maximum income income");
//Initializes the count value to "0"
int countValue = 0;
//Compute the count of families with incomes less than 10% of maximum income
for( i=0; i<incomeArr.size(); i++)
{
//Check condition
if(incomeArr.get(i) < (0.1 * maximumIncome))
{
//Increment count value
countValue++;
/* Display the family income with less than 10 percentage of income by family wise */
System.out.println("Family " + (i+1) + " had income " + incomeArr.get(i));
}
}
//Display count of families
System.out.println("Count of families with 10% of maximum income is: " + countValue);
}
}
Output:
Enter the income for each family. Use a negative value to indicate the end
Enter Income for family 1: 10000
Enter Income for family 2: 48000
Enter Income for family 3: 20000
Enter Income for family 4: 12000
Enter Income for family 5: 300000
Enter Income for family 6: 40000
Enter Income for family 7: -1
The maximum income is: 300000.0
10% of maximum income is: 30000.0
Displaying all families with incomes less than 10% of the maximum income
Family 1 had income 10000.0
Family 3 had income 20000.0
Family 4 had income 12000.0
Count of families with 10% of maximum income is: 3
Want to see more full solutions like this?
Chapter 12 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Additional Engineering Textbook Solutions
Problem Solving with C++ (10th Edition)
Starting Out With Visual Basic (8th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
SURVEY OF OPERATING SYSTEMS
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
- Make the following game user friendly with GUI, with some simple graphics. The GUI should be in another seperate class, with some ImageIcon, and Game class should be added into the pane. The following code works as this: The objective of the player is to escape from this labyrinth. The player starts at the bottom left corner of the labyrinth. He has to get to the top right corner of the labyrinth as fast he can, avoiding a meeting with the evil dragon. The player can move only in four directions: left, right, up or down. There are several escape paths in all labyrinths. The player’s character should be able to moved with the well known WASD keyboard buttons. If the dragon gets to a neighboring field of the player, then the player dies. Because it is dark in the labyrinth, the player can see only the neighboring fields at a distance of 3 units. Cell Class: public class Cell { private boolean isWall; public Cell(boolean isWall) { this.isWall = isWall; } public boolean isWall() { return…arrow_forwardDiscuss the negative and positive impacts or information technology in the context of your society. Provide two references along with with your answerarrow_forwardA cylinder of diameter 10 cm rotates concentrically inside another hollow cylinder of inner diameter 10.1 cm. Both cylinders are 20 cm long and stand with their axis vertical. The annular space is filled with oil. If a torque of 100 kg cm is required to rotate the inner cylinder at 100 rpm, determine the viscosity of oil. Ans. μ= 29.82poisearrow_forward
- Make the following game user friendly with GUI, with some simple graphics The following code works as this: The objective of the player is to escape from this labyrinth. The player starts at the bottom left corner of the labyrinth. He has to get to the top right corner of the labyrinth as fast he can, avoiding a meeting with the evil dragon. The player can move only in four directions: left, right, up or down. There are several escape paths in all labyrinths. The player’s character should be able to moved with the well known WASD keyboard buttons. If the dragon gets to a neighboring field of the player, then the player dies. Because it is dark in the labyrinth, the player can see only the neighboring fields at a distance of 3 units. Cell Class: public class Cell { private boolean isWall; public Cell(boolean isWall) { this.isWall = isWall; } public boolean isWall() { return isWall; } public void setWall(boolean isWall) { this.isWall = isWall; } @Override public String toString() {…arrow_forwardPlease original work What are four of the goals of information lifecycle management think they are most important to data warehousing, Why do you feel this way, how dashboards can be used in the process, and provide a real life example for each. Please cite in text references and add weblinksarrow_forwardThe following is code for a disc golf program written in C++: // player.h #ifndef PLAYER_H #define PLAYER_H #include <string> #include <iostream> class Player { private: std::string courses[20]; // Array of course names int scores[20]; // Array of scores int gameCount; // Number of games played public: Player(); // Constructor void CheckGame(int playerId, const std::string& courseName, int gameScore); void ReportPlayer(int playerId) const; }; #endif // PLAYER_H // player.cpp #include "player.h" #include <iomanip> Player::Player() : gameCount(0) {} void Player::CheckGame(int playerId, const std::string& courseName, int gameScore) { for (int i = 0; i < gameCount; ++i) { if (courses[i] == courseName) { // If course has been played, then check for minimum score if (gameScore < scores[i]) { scores[i] = gameScore; // Update to new minimum…arrow_forward
- In this assignment, you will implement a multi-threaded program (using C/C++) that will check for Prime Numbers and Palindrome Numbers in a range of numbers. Palindrome numbers are numbers that their decimal representation can be read from left to right and from right to left (e.g. 12321, 5995, 1234321). The program will create T worker threads to check for prime and palindrome numbers in the given range (T will be passed to the program with the Linux command line). Each of the threads works on a part of the numbers within the range. Your program should have some global shared variables: • numOfPrimes: which will track the total number of prime numbers found by all threads. numOfPalindroms: which will track the total number of palindrome numbers found by all threads. numOfPalindromic Primes: which will count the numbers that are BOTH prime and palindrome found by all threads. TotalNums: which will count all the processed numbers in the range. In addition, you need to have arrays…arrow_forwardHow do you distinguish between hardware and a software problem? Discuss theprocedure for troubleshooting any hardware or software problem. give one reference with your answer.arrow_forwardYou are asked to explain what a computer virus is and if it can affect computer’shardware or software. How do you protect your computer against virus? give one reference with your answer.arrow_forward
- Distributed Systems: Consistency Models fer to page 45 for problems on data consistency. structions: Compare different consistency models (e.g., strong, eventual, causal) for distributed databases. Evaluate the trade-offs between availability and consistency in a given use case. Propose the most appropriate model for the scenario and explain your reasoning. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS31Z9qoHazb9tC440AZF/view?usp=sharing]arrow_forwardOperating Systems: Deadlock Detection fer to page 25 for problems on deadlock concepts. structions: • Given a system resource allocation graph, determine if a deadlock exists. If a deadlock exists, identify the processes and resources involved. Suggest strategies to prevent or resolve the deadlock and explain their trade-offs. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS31Z9qoHazb9tC440 AZF/view?usp=sharing]arrow_forwardArtificial Intelligence: Heuristic Evaluation fer to page 55 for problems on Al search algorithms. tructions: Given a search problem, propose and evaluate a heuristic function. Compare its performance to other heuristics based on search cost and solution quality. Justify why the chosen heuristic is admissible and/or consistent. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS31Z9qoHazb9tC440 AZF/view?usp=sharing]arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT