Write a program that prompts the user for a doubled string


1. Given the following variable definitions,
int x = 3;
double d = 2.0;

what are the types and values of the following expressions? (As an example, the expression x + 2 has type int and value 5.)
(a) 5 / x
(b) 5 / d
(c) 5 / 2 + 1
(d) 4 < x
(e) 5 % x
(f) 4 != x && 2.5 > d
(g) !(3 >= x || true)

2. Write a program (main method) that prompts the user for a "doubled" String, where every character is immediately duplicated. It prints the un-doubled version. Include a loop so that the user is repeatedly prompted. Below is an example transcript.

Please enter a doubled string:
aabbcc
Undoubled is abc
Do you want to play again? (y/n)
y
Please enter a doubled string: SShhaarrkk88mmee
Undoubled is Shark8me
Do you want to play again? (y/n)
n

Plus, extend the previous solution so that it verifies that the input is doubled. If so, it behaves as above.
If not, it prints an appropriate error message.

3. Rewrite the following two code fragments, replacing for loops with equivalent while loops.

(a)
for (int x = 0; x < max; x = x + 3)
{

System.out.println(Math.sqrt(x));
}

(b)
public void listFunction(ArrayList list)
{
for (String e : list)
{
System.out.println (e);
}
}

4. Below is the start of a definition for a Course class. Each Course stores its name, current enrollment, and maximum enrollment. Provide a complete class definition, including a Course constructor and methods so that the following code will compile:

Course introJava = new Course("Java Programming", 15, 25); System.out.println("Course currently has " + introJava.getCurrentEnrollment() + " students"); System.out.println("How many students to add?");
Scanner input = new
Scanner(System.in); int moreStudents = input.nextInt(); introJava.addStudents(moreStudents);
System.out.println("Course currently has " + introJava.getCurrentEnrollment() + "
students"); if (introJava.overFull()){
System.out.println("too many students!");
}

5. When the code fragment above is run, the output might look like:

Course currently has 15 students
How many students to add?
18
Course currently has 33 students too many students!


Public class Course
{
Private String name;
Private int enrollment;
Private int max_enrollment;

// fill in the rest...

}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Write a program that prompts the user for a doubled string
Reference No:- TGS02392047

Now Priced at $20 (50% Discount)

Recommended (95%)

Rated (4.7/5)