Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA 1. (6 points) Assume that you want a for loop to display: 14 21 28 35 42 Complete the following for loops to accomplish

JAVA

1. (6 points) Assume that you want a for loop to display:

14

21

28

35

42

Complete the following for loops to accomplish this task.

a.

for (int i = 14; i <= 42; ________)

System.out.println(i);

b.

for (int i = _____; i < _______; i++)

System.out.println(i * 7);

2. (8 points) Write a code segment which prompts the user to enter the price of a tour and receives input from the user. A valid tour price is between $52.95 and $259.95, inclusive. Your program should continue to repeat until a valid tour price has been entered. If the user enters an invalid price, display a message describing why the input is invalid. You may assume that the user only enters a real number. The user will not enter any invalid characters. Although the use of constants to test the minimum and maximum price of a tour would be appropriate if this were part of a program, it is not necessary to take time declaring constants for the purposes of this exam.

3. (8 points) Assume you have the following declaration:

String sentence = new String(She sells seashells by the seashore.);

What is output by each of the following:

System.out.println(sentence.toUpperCase());

System.out.println(sentence.substring(12,15));

4. (10 points) Write a code segment that prompts the user to enter a sentence and then displays all of the words in the sentence which have an odd number of letters in them. You may assume that the user will input a string consisting of only English language words each separated by exactly one blank space and that the only punctuation symbol will be a period at the end of the sentence.

5. Assume that you are creating a class which defines a Garment object. Your class has the following variables:

private int garType;

private int garColor;

a. (6 points) Code the constructor for the Garment class which has two formal parameters whose values are assigned into the class variables.

b. (4 points) Code a mutator method for the garType variable.

c. (8 points) Assume that the garType variable contains a 1 for shirt and a 2 for pants, no other values will be loaded into this variable. Likewise the garColor variable contains a 1, 2 or 3 for red, green and blue respectively. Code the toString method for the Garment class which returns a string of the form: color space type. For example if garType contains a 1 (for a shirt) and garColor contains a 2 (for green) the method should return: green shirt

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_2

Step: 3

blur-text-image_3

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

Students also viewed these Databases questions