Modify the program so that it uses a loops repeatedly
doing:
• prompt for input (using line seqNum and the prompt phrase: "Enter
a line")
• reads the line of input
• stores the line of input in the next position of an array of Line
or
an ArrayList of Line.
Once the user enters STOP as input, the loop should terminate and
another loop should
print each line in reverse order. For example (the input you should
type in is in bold):
1: Enter a line
Some input
2: Enter a line
More input
3: Enter a line
The end
4: Enter a line
STOP
3: The end
2: More input
1: Some input
Hints:
To create an array of Line and add to it, assuming there will be no
more than 10 lines, use:
Line[] lineStore = new Line[10];
...
lineStore[n] = msg; // where msg is an object of type Line
...
lineStore[0]; // to get the Line object at position 0;
or to create an ArrayList of Line use:
ArrayList<Line> lineStore = new
ArrayList<Line>();
...
lineStore.add(msg); // where msg is an object of type Line
...
lineStore.get(0); // to get the Line object at position 0
My previous code:
public class P1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
final int maxnum = 3;
Line[] lineStore = new Line[maxnum];
Scanner in = new Scanner(System.in);
for (int i = 0; i < maxnum; i++) {
String msg = in.nextLine();
lineStore[i] = new Line(i + 1, msg);//start with 1 now
}
for (int i = lineStore.length-1; i >= 0; i--) {//i = 2
if (i != 0) {
System.out.print(lineStore[i].getSeqNum() + ": " +
lineStore[i].getText()+ ",");
} else {
System.out.print(lineStore[i].getSeqNum() + ": " +
lineStore[i].getText());
}
}
}
public class Line {
private int seqNum;
private String text;
public Line(int seqNum, String text) {
this.seqNum = seqNum;
this.text = text;
}
public int getSeqNum() {
return seqNum;
}
public String getText() {
return text;
}
}
//Java program
import java.util.ArrayList;
import java.util.Scanner;
class Line {
private int seqNum;
private String text;
public Line(int seqNum, String text) {
this.seqNum = seqNum;
this.text = text;
}
public int getSeqNum() {
return seqNum;
}
public String getText() {
return text;
}
}
public class P1 {
public static void main(String[] args) {
ArrayList<Line> lineStore
= new ArrayList<Line>();
Scanner in = new
Scanner(System.in);
int i=1;
boolean valid=true;
do {
System.out.println(i+":Enter a line");
String msg =
in.nextLine();
if(msg.compareToIgnoreCase("STOP")==0)
valid = false;
if(valid)lineStore.add( new Line(i, msg));
i++;
}while(valid);
for ( i = lineStore.size()-1; i
>= 0; i--)
System.out.println(lineStore.get(i).getSeqNum()
+ ": " + lineStore.get(i).getText());
in.close();
}
}
//sample output

Modify the program so that it uses a loops repeatedly doing: • prompt for input (using...
Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...
Java Programming Language Edit and modify from the given code Perform the exact same logic, except . . . The numeric ranges and corresponding letter grade will be stored in a file. Need error checking for: Being able to open the text file. The data makes sense: 1 text line of data would be 100 = A. Read in all of the numeric grades and letter grades and stored them into an array or arraylist. Then do the same logic....
Open a new file in your text editor, and start a class that will demonstrate a working two-dimensional array: import java.util.Scanner; class TwoDimensionalArrayDemo { public static void main(String[] args) { 2. Declare a three-by-three array of integers. By default, the elements will all be initialized to 0. int[][] count = new int[3][3]; 3. Declare a Scanner object for input, variables to hold a row and column, and a constant that can be used to indicate when the user wants to...
Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.print("There are 5 humans.\n"); array(); } public static String[] array() { //Let the user...
Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to sort." The actual question: Write a program as follows: Declare a five element array of Strings. Use a for loop and keyboard input to populate the array with the names of five friends. Sort the array in alphabetical order. Use a foreach loop to print the sorted array of friends, all on one line. Sample Output (inputs in italics) Enter the names of five...
Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the mean and standard deviation of these numbers I already have the source code all done. I just need this translated into a flowchart. Again I just need the flowchart, not any source code. I have already provided the source code below. Need Flowchart version of my code Here is the source code: public class Mean_Standard_Deviation { public static void main(String[] args) { ...
I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...
Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following: The instance variables for the class (recipeName, serving size, and...
Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...
Write a Java method that should take an ArrayList as a parameter, print its element in the reverse order. The main function that calls the method is the following: import java.util.*; public class ReverseGen { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("How many numbers do you want to input?"); int num = input.nextInt(); ArrayList<Double> d = new ArrayList<Double>(num); for(int i = 0 ; i < num; i++){ System.out.print("Enter...