Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need as a Java Program 2. Implement a method called checkAge after the main method as follows: public class Lab4Part1 { public static void main(String[]

Need as a Java Program

image text in transcribedimage text in transcribedimage text in transcribed

2. Implement a method called checkAge after the main method as follows: public class Lab4Part1 { public static void main(String[] args) { //Implementation of check age goes here 3. The checkAge method takes one input parameter of type int. The method then prints a message on the screen to tell whether the input age is minor (= 18 and =65). 4. In the main method, write statement to ask the user for his age, read the age using a Scanner, and then call the checkAge method with the user's age as a parameter. Test your program. 5. Follow the following steps to change the definition of the checkAge method such that instead of writing a message on the screen, the method returns a String as output. a. Change the method header by setting the output data type to String instead of void. b. Declare a String variable, called output, inside the checkAge method. c. Change the body of the if-else branches to assign the string to the variable output instead of writing the message on the screen. For example, the first body of the first branch will be as follows: output = "Minor"; d. Add a return statement at the end of the checkAge method to return the output string. Part 2. Writing methods in Java 1. In this part, you will implement a second method in the 'Lab4Part1' class. 2. Implement a method called multiconcat, that takes two input parameters where one is of type String and the other is of type int. The method returns as output a string that is equal to the input String parameter concatenated with itself n number of times (where n is the int parameter). For example, if the parameters are "hi" and 4, then the return value is "hihihihi". 3. Divide your work into step as follows: a. Write the method header. You need to answer the following questions to find out the different parts of the method header: i. What is the data type of the method output? ii. What is the method name? iii. What is the data type of the first input parameter? iv. What is the data type of the second input parameter? b. Write the method body as follows: i. Declare a local String variable that will be used to hold the output. ii. Write a for loop and the number of iterations should equal to the method's second input parameter. iii. Inside the for loop's body, append the first input parameter the output string. iv. Write a return statement to return the output string. c. Add statements in the main method to test your method as follows: i. Ask the user to enter a string and read the string. ii. Ask the use to enter the number of repetitions and read the number. iii. Call the multiconcat method with the user input as parameters. iv. Print the output of the method on the screen. Part 3. Methods and Loops 1. In this part, you are asked to change the main method of Part 2 so that the method calls the mutliConcat several times based on the user interest. 2. In order to implement this part, you need to write a while loop that is similar to the sentinel- controlled while loop that you wrote in Part 2 of Lab 2. Basically, the loop that asks the user whether she wants to run more tests and accepts the user response as "Y" or "N". If the user enters "Y", then your code asks the user to enter another String and int and uses these two inputs to call the multiConcat method

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions