Question

Urgent : need to create a java code that has an utput like this ----------------------------------------------------------------------- Gender...

Urgent : need to create a java code that has an utput like this

-----------------------------------------------------------------------

Gender % Under 30 % 30 or Over % Total

--------------------------------------------------------------------------------------------------

Female 44 17 61

Male 23 16    39'

--------------------------------------------------------------------------------------------------

Total 67 33 100

The percentages in the table are rounded to the nearest whole number and are percentages of all those who watch the show regularly. Thus, the sum of percentages in the last column of the table must be 100% (ignoring any rounding error).

Sample output

Please enter the person’s age (1..110): 25

Please enter the person’s gender (M/F): M

Please enter whether the person watches the show regularly (Y/N): Y

*displays table*

Do you want to enter another person’s details (Y/N)? if yes then loops

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

Here is the full code of file Main.java:

import java.util.Scanner;
import java.lang.Math;

public class Main
{
public static void main(String args[])
{
// Using Scanner for Getting Input from User
  
Scanner in = new Scanner(System.in);
int age = 0;
String gender = "";
String ans_show = "N";
String ans_again = "N";
int male_tot_under30 = 0;
int female_tot_under30 = 0;
int male_tot_above_or_equal30 = 0;
int female_tot_above_or_equal30 = 0;
int total_person = 0;
  
int per_u_m = 0;
int per_a_m = 0;
int per_u_f = 0;
int per_a_f = 0;
  
while(true) {
System.out.print("Please enter the person’s age (1..110):");
age = in.nextInt();
  
System.out.print("Please enter the person’s gender (M/F):");
gender = in.next().toUpperCase();
  
System.out.print("Please enter whether the person watches the show regularly (Y/N):");
ans_show = in.next().toUpperCase();
  
if(ans_show.equals("Y")) {
if(gender.equals("M")) {
if(age < 30) {
male_tot_under30++;
} else {
male_tot_above_or_equal30++;
}
} else if(gender.equals("F")) {
if(age < 30) {
female_tot_under30++;
} else {
female_tot_above_or_equal30++;
}
}
total_person = male_tot_under30 + male_tot_above_or_equal30 + female_tot_under30 +female_tot_above_or_equal30;
  
// Display the show_table
System.out.println("-----------------------------------------------------------------------");
System.out.println("Gender % Under 30 % 30 or Over % Total");
System.out.println("------------------------------------------------------------------------");
  
if(total_person > 0) {
per_u_f = Math.round(((float)female_tot_under30/total_person)*100);
per_a_f = Math.round(((float)female_tot_above_or_equal30/total_person)*100);
}
System.out.println("Female " + per_u_f + " " + per_a_f + " " + (per_u_f + per_a_f));
  
if ((male_tot_under30 + male_tot_above_or_equal30) > 0) {
per_u_m = Math.round(((float)male_tot_under30/total_person)*100);
per_a_m = Math.round(((float)male_tot_above_or_equal30/total_person)*100);
}
System.out.println("Male " + per_u_m + " " + per_a_m + " " + (per_u_m + per_a_m));
  
System.out.println("------------------------------------------------------------------------");
System.out.print("Total " + (per_u_m+per_u_f) + " " + (per_a_m+per_a_f) + " ");
if ((per_u_m + per_a_m + per_u_f + per_a_f) > 100 ) {
System.out.print("100\n");
} else {
System.out.print((per_u_m + per_a_m + per_u_f + per_a_f) + "\n");
}
}
  
System.out.print("Do you want to enter another person’s details (Y/N):");
ans_again = in.next().toUpperCase();
  
//System.out.println("You entered.." + age + gender + ans_show + ans_again);
//System.out.println("male female " + male_tot_under30 + " " + male_tot_above_or_equal30 + " " + female_tot_under30 + " " + female_tot_above_or_equal30);
  
if(ans_again.equals("N")) {
System.out.println("....Program ends here....");
break;
}
}
}
}

After compilation run the Main class
The sample output as below

Please enter the person’s age (1..110):6
Please enter the person’s gender (M/F):M
Please enter whether the person watches the show regularly (Y/N):Y
-----------------------------------------------------------------------
Gender % Under 30 % 30 or Over % Total
------------------------------------------------------------------------
Female 0 0 0
Male 100 0 100
------------------------------------------------------------------------
Total 100 0 100
Do you want to enter another person’s details (Y/N):Y
Please enter the person’s age (1..110):66
Please enter the person’s gender (M/F):F
Please enter whether the person watches the show regularly (Y/N):Y
-----------------------------------------------------------------------
Gender % Under 30 % 30 or Over % Total
------------------------------------------------------------------------
Female 0 50 50
Male 50 0 50
------------------------------------------------------------------------
Total 50 50 100
Do you want to enter another person’s details (Y/N):N
....Program ends here....


Add a comment
Know the answer?
Add Answer to:
Urgent : need to create a java code that has an utput like this ----------------------------------------------------------------------- Gender...
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
  • Please write down the code in JAVA. Objectives .To gain experience with using recursion by implementing...

    Please write down the code in JAVA. Objectives .To gain experience with using recursion by implementing the Ackermann's Function. To gain experience with using class variables to record method invocation history Description The Ackermann's function is of interest because it grows rapidly with respect to the size of m and n. It is the simplest example of a well-defined total function which is computable but not primitive recursive. This means it cannot be implemented using only for-loops. Notice that for-...

  • PLEASE I NEED HELP WITH THIS JAVA CODE Homework 3-3 Create a Driver class to use...

    PLEASE I NEED HELP WITH THIS JAVA CODE Homework 3-3 Create a Driver class to use your Airplane and Passenger classes and create instances of them. In the main method do the following: Create an Airplane that will store up to 100 Passengers Create 5 Passenger Objects with the details specified in the table below Add the 5 Passenger objects to the Airplane Call the printDetails method from the Airplane to print all the Airplane and Passenger details. variable name...

  • What is the java code for this. The goal of this lab is to write two...

    What is the java code for this. The goal of this lab is to write two programs, Summation and Prime, that execute simple tasks. The first computes the summation of integers within a range with a gap that the user specifies. The second tests whether the integer that the user enters is a square and if not, whether it is composite or prime. Summation Let us see some execution examples first, to get the sense of how the program works....

  • I should use the array and loop to create a java program according to the instruction,...

    I should use the array and loop to create a java program according to the instruction, but I have no idea how to do it. Introduction This lab assignment continues to give you practice using loops, particularly loops with variable termination conditions, and it also provides you an opportunity to use one-dimensional arrays. Recall that an array is used to store a collection of data. The data can be values of Java primitive data types or else objects (for instance,...

  • eclipse java Oxygen Design a self-service fast food menu. Name your class FastFood. Create a test...

    eclipse java Oxygen Design a self-service fast food menu. Name your class FastFood. Create a test class based on the examples from chapter 4. Create a program to do the following. When you set up the program, save the java file to a folder named Chapter 04 Program. Console Welcome to the fast food order menu How may I help you Please place your order: НННН Please place your order Your order Your subtotal: $e.00 $e.00 Continue? (y/n): Y Please...

  • Need help answering this java programming question. Thank you in advance! g. Re-code the following as...

    Need help answering this java programming question. Thank you in advance! g. Re-code the following as a do-while loop with a switch statement for the if-elses, Assume goingToMovies is declared and has been set to true, so it can enter the loop. Assume choice, movie, Cont and input are already declared. while(goingToMavies) Sustem.aut printf("%nChoose a number from 1 through 5 for the movie" + "you want to watch: ") choice inputnextint) iffchoice 1) movie "Shazam!" else iffchoice 2) movie "A...

  • Need java code for both questions..thx > Problem 1 U As you know the ber of...

    Need java code for both questions..thx > Problem 1 U As you know the ber of days in achthof our ca . Forway 2 days in a year, or days other • April S a d November 30 days. • All other s ide y that are divisie by deg 200 2012, 2016) How the years that are dividely 100 g 2100, 2200) e t lap years. But there's a lot ption years that are dive ly 400 ... 1600,...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

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
ADVERTISEMENT