Question

// This program displays a menu and asks the user to make a 2 // selection....

// This program displays a menu and asks the user to make a
2 // selection. An if/else if statement determines which item
3 // the user has chosen.
4 #include <iostream>
5 #include <iomanip>
6 using namespace std;
7
8 int main()
9 {
10 int choice; // To hold a menu choice
11 int months; // To hold the number of months
12 double charges; // To hold the monthly charges
13
14 // Constants for membership rates
15 const double ADULT = 40.0,
16 SENIOR = 30.0,
17 CHILD = 20.0;
18
19 // Constants for menu choices
20 const int ADULT_CHOICE = 1,
21 CHILD_CHOICE = 2,
22 SENIOR_CHOICE = 3,
23 QUIT_CHOICE = 4;
24
25 // Display the menu and get a choice.
26 cout << "\t\tHealth Club Membership Menu\n\n"
27 << "1. Standard Adult Membership\n"
28 << "2. Child Membership\n"
29 << "3. Senior Citizen Membership\n"
30 << "4. Quit the Program\n\n"
31 << "Enter your choice: ";
32 cin >> choice;
33
34 // Set the numeric output formatting.
35 cout << fixed << showpoint << setprecision(2);
36
37 // Respond to the user's menu selection.
38 if (choice == ADULT_CHOICE)
39 {
40 cout << "For how many months? ";
41 cin >> months;
42 charges = months * ADULT;
43 cout << "The total charges are $" << charges << endl;
44 }
(program continues)
M04_GADD6253_07_SE_C04 Page 191 Tuesday, January 4, 2011 9:03 PM
192 Chapter 4 Making Decisions
Let s take a closer look at the program:
Lines 10 12 define the following variables:
The choice variable will hold the user s menu choice
The months variable will hold the number of months of health club membership
The charges variable will hold the total charges
Lines 15 17 define named constants for the monthly membership rates for adult,
senior citizen, and child memberships.
Lines 20 23 define named constants for the menu choices.
Lines 26 32 display the menu and get the user s choice.
Line 35 sets the numeric output formatting for floating point numbers.
45 else if (choice == CHILD_CHOICE)
46 {
47 cout << "For how many months? ";
48 cin >> months;
49 charges = months * CHILD;
50 cout << "The total charges are $" << charges << endl;
51 }
52 else if (choice == SENIOR_CHOICE)
53 {
54 cout << "For how many months? ";
55 cin >> months;
56 charges = months * SENIOR;
57 cout << "The total charges are $" << charges << endl;
58 }
59 else if (choice == QUIT_CHOICE)
60 {
61 cout << "Program ending.\n";
62 }
63 else
64 {
65 cout << "The valid choices are 1 through 4. Run the\n"
66 << "program again and select one of those.\n";
67 }
68 return 0;
69

----

how can i write a program like this but in java.

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

import java.io.*; //importing all I/O libraries
public class Health //name of the class
{
public static void main(String args[])throws IOException
{
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));   /*BufferedReader is a class in Java that reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, lines and arrays*/
   int choice,months;
   double charges;
   final double adult=40; //instead of const keyword in C++, we use final. final acts as const in java
   final double senior=30;
   final double child=20;
   final int adult_choice=1;
   final int child_choice=2;
   final int senior_choice=3;
   final int quit_hoice=4;
   System.out.println( "\t\tHealth Club Membership Menu\n\n1. Standard Adult Membership\n2. Child Membership\n3. Senior Citizen Membership\n4. Quit the Program\n\nEnter your choice: ");
   System.out.println();
   choice=Integer.parseInt(br.readLine()); //Input as Integer.
   if(choice==adult_choice)
   {
       System.out.println("For how many months?");
       months=Integer.parseInt(br.readLine());
       charges=months*adult;
       System.out.print("The total charges are: $");
       System.out.print(charges);
       System.out.println();
   }
   else if(choice==child_choice)
   {
       System.out.println("For how many months?");
       months=Integer.parseInt(br.readLine());
       charges=months*child;
       System.out.print("The total charges are: $");
       System.out.print(charges);
       System.out.println();
   }
   else if(choice==senior_choice)
   {
       System.out.println("For how many months?");
       months=Integer.parseInt(br.readLine());
       charges=months*senior;
       System.out.print("The total charges are: $");
       System.out.print(charges);
       System.out.println();
   }
   else if(choice==quit_hoice)
   {
       System.out.println("Program Ending\n");
   }
   else
   {
       System.out.println("The valid choices are 1 through 4. Run the program again and select one of those.");
   }

}
}

Output:

Note:

To compile a program in java through cmd use keyword: javac filename.java

then type

java filename

Hope this helps you,

Thank You.

Add a comment
Know the answer?
Add Answer to:
// This program displays a menu and asks the user to make a 2 // selection....
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
  • Plz give me correct code and screen shots for this question by using SASM !!!!!!!! Implement...

    Plz give me correct code and screen shots for this question by using SASM !!!!!!!! Implement the following working C++ program in assembler. Submit assembler code and screen shot. #include <iostream> #include <iomanip> using namespace std; // This menu-driven Health Club membership program carries out the // appropriate actions based on the menu choice entered. A do-while loop // allows the program to repeat until the user selects menu choice 4. int main() { // Constants for membership rates const...

  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • Can someone please tell me why the calculation for earnings is not coming through at the...

    Can someone please tell me why the calculation for earnings is not coming through at the end of the program? Thank you. //This program should tell us about streaming services #include <iostream> #include <iomanip> #include <string> using namespace std; int main() {    int choice, streams;    double earnings;    string song;    string artist;    string streamingService;       const int Tidal = 0.01250;    const int Amazon = 0.00402;    const int Apple_Music = 0.00735;    const int...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • My if/else statement wont run the program that I am calling. The menu prints but once...

    My if/else statement wont run the program that I am calling. The menu prints but once I select a number the menu just reprints, the function called wont run. What can I do to fix this probelm? #include <iostream> #include "miltime.h" #include "time.h" #include "productionworker.h" #include "employee.h" #include "numdays.h" #include "circle.h" using namespace std; int main() { int select=1;     while (select>0) { cout << "Option 1:Circle Class\n"<< endl; cout << "Option 2:NumDay Class\n" << endl; cout <<"Option 3:Employee and Production...

  • I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for.....

    I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for..while..do while,loops,pointer,address,continue,return,break. Create a C++ program which contain a function to ask user to enter user ID and password. The passwordmust contain at least 6 alphanumeric characters, 1 of the symbols !.@,#,$,%,^,&,* and 1 capital letter.The maximum allowable password is 20. Save the information. Test the program by entering the User ID and password. The...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • Can you fix this program and run an output for me. I'm using C++ #include using...

    Can you fix this program and run an output for me. I'm using C++ #include using namespace std; //function to calculate number of unique digit in a number and retun it int countUniqueDigit(int input) {    int uniqueDigitCount = 0;    int storeDigit = 0;    int digit = 0;    while (input > 0) {        digit = 1 << (input % 10);        if (!(storeDigit & digit)) {            storeDigit |= digit;       ...

  • Question 2: This program continue asking for a new number until the user enters a 0...

    Question 2: This program continue asking for a new number until the user enters a 0 to terminate the program #include <iostream.h> using namespace std; int main(void) {         int x;         int count = 0;   // (1) initialize a counter to 0 to count number of values         int choice = 1; // This is the choice that controls the looping continuation or termination         double sum = 0; // initialize the sum to 0 to make sure the...

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