Question

Methods and File Output and Exceptions Outcome: Student will demonstrate the ability to write, use and...

Methods and File Output and Exceptions

Outcome:

  • Student will demonstrate the ability to write, use and call methods
  • Student will demonstrate the ability to pass values to and from methods
  • Student will demonstrate the ability to catch exceptions
  • Student will demonstrate the ability to create a text file
  • Student will demonstrate the ability to validate input data

Program Specifications:

You to write a menu driven program. The program will use a switch statement. The cases will be as follows:

  1. Get the user’s first name and echo it back out 20 times.
  2. Get the Store user’s age and double it and display the age and the doubled age.
  3. Using the age from #2 output one of the following statements
    1. Since you are XX years old, you are a teenager
    2. Since you are XX years old, you are NOT a teenager
  4. Get a single integer between 3 and 50 from the user. Create a triangle of X’s with the integer inputted rows. The triangle needs to be displayed on the screen and in a text document named triangle.txt.

Submission Requirements:

  • You must follow the rules from the second assignment.

YOU CANNOT:

  • Use global variables
  • Use the word goto
  • Use the break command outside a case statement
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

//Menu.java

import java.util.Scanner;

public class Menu {

public static void main(String[] args) {
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
int choice;
do
{
System.out.println("\n::MENU::");
System.out.println("1.Echo Name");
System.out.println("2.Double Your Age");
System.out.println("3.Check Teenager or not");
System.out.println("4.Display Triangle");
System.out.println("5.Exit");
System.out.print("Enter Choice:");
choice = sc.nextInt();
switch (choice) {
case 1: {
System.out.print("Enter your name");
String name=sc.nextLine();
for(int i=1;i<=20;i++)
{
System.out.println(name);
}
break;
}
case 2: {
int age;
System.out.print("Enter age :");
age=sc.nextInt();
System.out.println("Your age :"+age);
System.out.println("Doubled age :"+(2*age));
  
  
  
break;
}
case 3: {
int age;
System.out.print("Enter age :");
age=sc.nextInt();
  
if(age>=13 && age<=19)
{
System.out.println("Since you are "+age+" years old.Your are a Teenager");
}
else
{
System.out.println("Since you are "+age+" years old.Your are not a Teenager");
}
break;
}
case 4: {
int rows;
do
{
System.out.print("Enter a number (between 3 and 50):");
rows=sc.nextInt();
if(rows<3 || rows>50)
{
System.out.println("** Invalid.Must be between 3 and 50 **");
}
}while(rows<3 || rows>50);
printTriangle(rows);
break;
}
case 5:{
break;
}
default: {
System.out.println("** Invalid Choice **");
break;
}

}  
}while(choice!=5);
  

}

private static void printTriangle(int size) {
char ch='*';
for (int a = 0; a < 2 * size - 1; a++) {
if (a % 2 == 0) {
for (int b = 0; b < (2 * size - 1) - a; b++) {
System.out.print(" ");
}

for (int c = 0; c <= a; c++) {
System.out.print(ch+" ");
}
System.out.println();
}

}
  
}

}

___________________________

Output:


::MENU::
1.Echo Name
2.Double Your Age
3.Check Teenager or not
4.Display Triangle
5.Exit
Enter Choice:1
Enter your name:Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams
Williams

::MENU::
1.Echo Name
2.Double Your Age
3.Check Teenager or not
4.Display Triangle
5.Exit
Enter Choice:2
Enter age :23
Your age :23
Doubled age :46

::MENU::
1.Echo Name
2.Double Your Age
3.Check Teenager or not
4.Display Triangle
5.Exit
Enter Choice:3
Enter age :16
Since you are 16 years old.Your are a Teenager

::MENU::
1.Echo Name
2.Double Your Age
3.Check Teenager or not
4.Display Triangle
5.Exit
Enter Choice:4
Enter a number (between 3 and 50):6
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *

::MENU::
1.Echo Name
2.Double Your Age
3.Check Teenager or not
4.Display Triangle
5.Exit
Enter Choice:5

##Please like rate to my answer ,very helpful for me

Add a comment
Know the answer?
Add Answer to:
Methods and File Output and Exceptions Outcome: Student will demonstrate the ability to write, use and...
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
  • Java Programing Code only Ragged Array Assignment Outcome: Student will demonstrate the ability to create and...

    Java Programing Code only Ragged Array Assignment Outcome: Student will demonstrate the ability to create and use a 2D array Student will demonstrate the ability to create and use a jagged array Student will demonstrate the ability to design a menu system Student will demonstrate the ability to think Program Specifications: Write a program that does the following: Uses a menu system Creates an array with less than 25 rows and greater than 5 rows and an unknown number of...

  • ***************C PROGRAMMING ONLY************* Demonstrate the ability to create an array on the stack Demonstrate the ability...

    ***************C PROGRAMMING ONLY************* Demonstrate the ability to create an array on the stack Demonstrate the ability to create an array on the heap allowing user to choose the number of values to store. Demonstrate the ability to store an array of Struct values on both the stack and the heap. Program Specifications: 1. Create a struct with at least 3 fields - any struct you want but explain it in your comments in the code. Populate at least 10 elements...

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

  • Java Exceptions Suppose a library is processing an input file containing the titles of books in...

    Java Exceptions Suppose a library is processing an input file containing the titles of books in order to remove duplicates. Write a program that reads all of the titles from an input file called bookTitles.inp and writes them to an output file called uniqueTitles.out. When complete, the output file should contain all unique titles found in the input file. Create the input file using Notepad or another text editor, with one title per line. Make sure you have a number...

  • (Java) Rewrite the following exercise below to read inputs from a file and write the output...

    (Java) Rewrite the following exercise below to read inputs from a file and write the output of your program in a text file. Ask the user to enter the input filename. Use try-catch when reading the file. Ask the user to enter a text file name to write the output in it. You may use the try-with-resources syntax. An example to get an idea but you need to have your own design: try ( // Create input files Scanner input...

  • Can you please pick Automobile For this assignment. Java Programming Second Inheritance OOP assignment Outcome: Student...

    Can you please pick Automobile For this assignment. Java Programming Second Inheritance OOP assignment Outcome: Student will demonstrate the ability to use inheritance in Java programs. Program Specifications: Programming assignment: Classes and Inheritance You are to pick your own theme for a new Parent class. You can pick from one of the following: Person Automobile Animal Based on your choice: If you choose Person, you will create a subclass of Person called Student. If you choose Automobile, you will create...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling...

    Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling and Input/Output. Using try/catch/finally statement to handle exceptions, or declare/throw an exception as needed, create the following program: Create an array of 25 random numbers between 0 & 250 formatted to a field width of 10 & 4 decimal places (%10.4f). Use the formula Math.random() * 250. Display the array of random numbers on the console and also write to a file. Prompt the...

  • Objective: Text File I/0 and Regular Expressions Note that both classes below should handle all exceptions...

    Objective: Text File I/0 and Regular Expressions Note that both classes below should handle all exceptions that might be thrown within them. 1. Create a class that does the following: a. Reads the name of a file to create as the first command line argument. (Overwrite any file with the same name). b. Reads an integer value as the second command line argument. C. The program should generate as many random numbers as are specified in the second command line...

  • Write a Java program that reads a file until the end of the file is encountered....

    Write a Java program that reads a file until the end of the file is encountered. The file consists of an unknown number of students' last names and their corresponding test scores. The scores should be integer values and be within the range of 0 to 100. The first line of the file is the number of tests taken by each student. You may assume the data on the file is valid. The program should display the student's name, average...

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