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
  • I need help with this Java problem, please. Code must be written in Java Create an...

    I need help with this Java problem, please. Code must be written in Java Create an application that calculates batting statistics for baseball players. Console Welcome to the Batting Average Calculator Enter number of times at bat: 5 0 = out, 1 = single, 2 = double, 3 = triple, 4 = home run Result for at-bat 1: 0 Result for at-bat 2: 1 Result for at-bat 3: 0 Result for at-bat 4: 2 Result for at-bat 5: 3 Batting...

  • I'm having trouble rounding the numbers i'm getting in my output here's my code: #include<stdio.h> int...

    I'm having trouble rounding the numbers i'm getting in my output here's my code: #include<stdio.h> int main() { char gender; float a1, a2, a3, a4, a5; float waistmeasurement, wristmeasurement, hipmeasurement, forarmmeasurement; float B, bodyweight, Bodyfat, Bodyfatpercentage;    printf("This program determines the body fat of a person.Enter your gender (f|F|m|M): "); scanf("%c", &gender); printf("\n");    if(gender=='F' || gender=='f'){ printf("Enter body weight (in pounds): \n"); scanf("%f", &bodyweight); printf("Enter wrist measurement at fullest point (in inches): \n"); scanf("%f", &wristmeasurement); printf("Enter waist measurement at...

  • 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...

  • I need help writing a code in Java or C or python that follows the following...

    I need help writing a code in Java or C or python that follows the following requirements. 1. It prompts the user to enter values of the four integers (a, b, c, and M in this order) and prints out the first 100 elements (?0, ?1, ?2, …, ?99) of the sequence produced by the linear congruential random number generator for these parameters. Use a for loop and the following update formula: x = (a * x + b) %...

  • 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,...

  • Hallo! I have a problem in my code, I need help please in java code. My...

    Hallo! I have a problem in my code, I need help please in java code. My task is : Write a program that can read a text from a file and do frequency analysis on the letters that occur . To store information (frequency) you must use an array. Tip! The place where you collect the frequency of eg the letter A is the equivalent of the ASCII code of "A" ie 65. The array should be 127 elements. For...

  • given code: assignment: use python to extend the given code to create an output similar to...

    given code: assignment: use python to extend the given code to create an output similar to the one in the black box please explain the final code import random HALMst N in = int(input('Enter Total Number of Cards: ") NM in NOON A = [] B [] E - [] D = [] E = [] for i in range(1, n+1): A.append(i) D.append(i) E.append(i) for j in range(n): 8.append(random.choice(D)) D.remove(B[5]) C.append(random.choice(E)) E.remove(C[j]) c= 0 for k in range(n): if (Brkl...cikl):...

  • 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,...

  • I need this done in C++ Can someone help me get my code from crashing. The...

    I need this done in C++ Can someone help me get my code from crashing. The code compiles but it can be easily crashed with user input. The rubric and code below Instructions: Write a program that scores the following data about a soccer player in a structure:            Player’s Name            Player’s Number            Points Scored by Player      The program should keep an array of 12 of these structures. Each element is for a different player on a...

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