Apple1210
Apple1210

Reputation: 1

Getter and setters and arrays in java

I am having an issue with a java task I'm doing at the moment. I am trying to answer question 3 and 4 which is based on the following code you see below .

After running the program, the output window should look like:

1: Enter a line
Some input
2: Enter another line
More input
3: Enter the last line
The end
The end,More input,Some input

import java.util.Scanner;
public class Practical1 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
String msg1, msg2, msg3;
System.out.println("Enter a line");
msg1 = in.nextLine();
System.out.println("Enter another line");
msg2 = in.nextLine();
System.out.println("Enter the last line");
msg3 = in.nextLine();
System.out.println(msg3 + "," + msg2 + "," + msg1) ; }}

The code above ansered question 1 and 2.

For question 3, its asking me to create a new java class as i did called Line and then Add appropriate instance variables (fields) and a constructor to the class so that a line of text (a String, called text) and a sequence number (an int, called seqNum) can be stored. These instance variables should be "read only" for other classes. That is, they should be declared "private" and a "getter" method provided to return the value. This is what i did in the code below:

public class Line {

public String getText() {

return text;

 }

   private String text;

   public int getSeqNum() {

    return seqNum;

}

private int seqNum;

}

It then asks to Modify the main method of the class Practical1 so that it stores each line of text and its sequence number in a Line object, rather than a String object. Modify the type of variables (msg1 etc.) as required. Note that to print out a line, you will need to call the getter method. The output of your program should be exactly as before.

The end,More input,Some input

Finally it asks you to Now change the program so that the sequence number is printed with the line. The final line of output should be:

 3: The end,2: More input,1: Some input

How would i do this question?

For question 4 the last question, it wants me to 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

How would I do this question ?

Upvotes: 0

Views: 290

Answers (1)

user8838577
user8838577

Reputation:

You can to following approach, to solve this problem

First the Line class

public class Line
{
    private int seq_num;
    private String text;
    Line(int seq_num, String text)
    {
        this.seq_num = seq_num;
        this.text = text;
    }
    public String getText()
    {
        return this.text;
    }
    public int getSeq()
    {
        return this.seq_num;
    }
}

Demo class for taking infinite input until STOP text is provided

class Demo
{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<Line> list = new ArrayList<>();
        int counter =0;
        while(true)
        {
            String scanned_line = sc.nextLine();
            if(scanned_line.equals("STOP"))
                break;

            list.add(new Line(counter,scanned_line));
            counter++;
        }
        //sort the list as per the seq num in reverse order 
        list.sort(Comparator.comparingInt(Line::getSeq).reversed()); // java 8 comparator
        list.forEach(e-> System.out.println(e.getSeq()+" "+e.getText()));//java 8 forEach
    }
}

Also ,

  • important to mention that while(true) can be replace with while(scanner.hasNextLine()) . so it will scan until it has next line input provided.
  • Also java 8 forEach can be replaced with traditional for loop.

Upvotes: 1

Related Questions