Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1.) Spot and correct at least five errors in the following program. Start at the very beginning and move forward, looking for errors and assuming

1.) Spot and correct at least five errors in the following program. Start at the very beginning and move forward, looking for errors and assuming that your previous corrections apply, at that point.

Line #01: import java.util.random; Line #02: Line #03: public class 1stFloatClass { Line #04: Line #05: public static void main(String[] args) { Line #06: Line #07: Scanner scan = new Scanner(System.in); Line #08: double[] doubles = new double(); Line #09: int x = 0.0; Line #10: Line #11: for (i = 0; i < doubles.length(); i++) { Line #12: doubles[i] = I * 1.7; Line #13: System.out.print("Enter an int value: "); Line #14: x = scan.next(); Line #15: System.out.println("You entered: " + x); Line #16: System.out.println("Computer says: " + doubles{i} + x; Line #17: } Line #18: Line #19: }

2.)

Write a program that will...

Ask the user for the number of lines,

Store that value in the variable "lines" (already done for you!),

And print out each line.

To print a single line, you should do the following:

Repeatedly print a random character to the line. To do this, you can call the randChar() method. The number of characters printed should be equal to half of the line number. (Notice, in the case of line number 1, that no characters are printed. This is because integer division of 1 by 2 yields a result of zero.)

Skip to the next line.

As you might be able to tell, this is a "nested loop" problem!

NOTE: Remember the distinction between "print" vs. "println"

import java.util.Scanner; public class PrintingChars{ public static void main (String[] args) { Scanner scan = new Scanner(System.in); System.out.println("How many lines?"); int lines = scan.nextInt(); // For each line, between one and the // value of "lines" // Print out a number of characters // equal to one half the line # // Skip to the next line. 

 } // You can call this method to get a random character private static char randChar(){ java.util.Random rGen = new java.util.Random(); return (char) (rGen.nextBoolean() ? randomIntInRange('a', 'z') : randomIntInRange('0', '9')); } private static int randomIntInRange(int low, int high) { int multiplier = high - (low - 1); return (int)(Math.random() * multiplier) + low; } }

Sample output:

How many lines? 20 Line 1: Line 2: i Line 3: w Line 4: f6 Line 5: ao Line 6: pp1 Line 7: 1e9 Line 8: 1t84 Line 9: 870m Line 10: hjc7w Line 11: a76e2 Line 12: 4c51wu Line 13: 05225s Line 14: 3g6z0y5 Line 15: b4bl4c8 Line 16: d752z92g Line 17: 7m13io15 Line 18: 52i893als Line 19: 50ieiw608 Line 20: wya52mi9jo

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

Students also viewed these Databases questions

Question

Consider the sequence {an given by n In(n)

Answered: 1 week ago

Question

The company openly shares plans and information with employees.

Answered: 1 week ago