Question

In: Computer Science

Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...

Module 1 Program

Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for the line. Treat the first line so that it has a timestamp of 0.00. The last line of the lyrics will have a timestamp. All the timestamps will be in double format. If a line has more than one timestamp, use the first timestamp found searching left to right for the outpt timestamp of that line. Some lines may not contain an embedded timestamp. Interpolate the timestamp so that it is evenly spaced from the preceding line that has a timestamp to next line which also has a timestamp. When displaying the output, display the timestamp for the line as double with 2 places past the decimal place. For example if lyrics.txt contains

Ay
Fonsi
5.02 DY
Oh, oh no, oh no
Oh, yeah
Diridiri, dirididi Daddy
Go
Si, sabes que ya llevo un rato mirandote
Tengo que bailar 10.4  contigo hoy (DY)
Vi que tu 12 mirada 12.1 ya estaba llamandome
Muestrame el camino que yo voy (oh)
Tu, tu eres el iman y yo soy el metal
Me voy acercando y 15.5 voy armando el plan

Then your program should print to the screen

0.00 Ay
2.51 Fonsi
5.02 DY
5.92 Oh, oh no, oh no
6.81 Oh, yeah
7.71 Diridiri, dirididi Daddy
8.61 Go
9.50 Si, sabes que ya llevo un rato mirandote
10.40 Tengo que bailar contigo hoy (DY)
12.00 Vi que tu mirada ya estaba llamandome
13.17 Muestrame el camino que yo voy (oh)
14.33 Tu, tu eres el iman y yo soy el metal
15.50 Me voy acercando y voy armando el plan
  1. If the file does not exist, then your program should instead display, “File not found” and exit.
  2. If the lyrics.txt contains more than 200 lines, it should display, “The song is too long” and exit.
  3. If the lyrics file is empty, the program should display, “Empty file” and exit.

A little starter code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * Program that reads lyrics from file lyrics.txt and prints the lyrics
 * with a timestamp to the left of each line

public class Module1Program {
    
    public static final int NUM_LINES = 200;
    

    public static void main(String[] args) {
        File theSong = new File("lyrics.txt");
        Scanner input;
        
        try {
            input = new Scanner(theSong);
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
            return;
        }
        
        while (input.hasNextLine()) {
            System.out.println(input.nextLine());
        }
        
        input.close();
    }

}

Make sure that your program does not have a Package.

Testing: You should test your program in a variety of situations – missing file, empty file, file too long, file with only one timestamp, file with multiple timestamps per line.

Solutions

Expert Solution

If you have any doubts, please give me comment...

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

/**

* Program that reads lyrics from file lyrics.txt and prints the lyrics with a

* timestamp to the left of each line

*/

public class Module1Program {

    public static final int NUM_LINES = 200;

    public static void main(String[] args) {

        File theSong = new File("lyrics.txt");

        Scanner input;

        try {

            input = new Scanner(theSong);

        } catch (FileNotFoundException e) {

            System.out.println("File not found");

            return;

        }

        if(!input.hasNextLine()){

            System.out.println("Empty file");

            return;

        }

        String lines = "";

        double prev_time, end_time, diff, time;

        prev_time = 0.0;

        time = 0.0;

        int n = 0;

        while (input.hasNextLine()) {

            String line = input.nextLine();

            String tokens[] = line.split(" ");

            // System.out.println(tokens.length);

            for (int i = 0; i < tokens.length; i++) {

                if (tokens[i].matches("[0-9]+(\\.[0-9]*)?")) {

                    end_time = Double.parseDouble(tokens[i]);

                    diff = (end_time - prev_time) / n;

                    time = prev_time;

                    // lines += " ";

                    for (int j = i; j < tokens.length; j++) {

                        if (!tokens[j].matches("[0-9]+(\\.[0-9]*)?")){

                            // if(input.hasNextLine())

                                lines += tokens[j];

                        }

                    }

                    // System.out.println(lines);

                    for (String str : lines.split("\n")) {

                        if(!str.isEmpty())

                            System.out.printf("%.2f %s\n", time, str.trim());

                        time += diff;

                    }

                    time -= diff;

                    // System.out.println("--------------------");

                    n = 0;

                    lines = "";

                    prev_time = end_time;

                    break;

                    // System.out.println(tokens[i]);

                } else

                    lines += tokens[i] + " ";

            }

            lines += "\n";

            n++;

        }

        // System.out.printf("%.2f %s\n", time, lines);

        input.close();

    }

}


Related Solutions

In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total number of numbers in the file, Calculate the sum of all the numbers in the file, Calculate the average of all the numbers in the file, Find the smallest value of all the numbers in the file, Find the largest value of all the numbers in the file, Calculate the standard deviation of all the numbers in the file
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
Write a program to complete the following tasks. Record all results to a data file called "GradeSheet.dat".
 c++Write a program to complete the following tasks. Record all results to a data file called "GradeSheet.dat".(1) Read the grade sheet from attached data file, "Assignment4.dat". Print the original grade sheet.(3) Write a module to sort array GRADE[] and print the sorted grade sheet.(4) Write a module to sort array NAME[] and print the sorted grade sheet.(5) Write a module to sort array ID[] and print the sorted grade sheet.(6) Write a module to print the student's id, name, and...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts...
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts them into a string array, and sorts the array in alphabetical order. String objects can be compared using relational operators such as <, >, or ==. For example, “abc” > “abd” is false, but “abc” < “abd” is true. Sample output: Before Sorting: Cherry, Honeydew, Cranberry, Lemon, Orange, Persimmon, Watermelon, Kiwifruit, Lime, Pomegranate, Jujube, Pineapple, Durian, Plum, Banana, Coconut, Apple, Tomato, Raisin, Mandarine, Blackberry,...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list of integers from use input and has the following methods: 1. min - Finds the smallest integer in the list. 2. max- Finds the largest integer in the list. 3. average - Computes the average of all the numbers. 4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list of integers from use input and has the following methods: 1. min - Finds the smallest integer in the list. 2. max- Finds the largest integer in the list. 3. average - Computes the average of all the numbers. 4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT