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 Java source file and produces an index of all identifiers...
Write a program that reads a Java source file and produces an index of all identifiers in the file. For each identifier, print all lines in which it occurs. For simplicity, we will consider each string consisting only of letters, numbers, and underscores an identifer. Declare a Scanner in for reading from the source file and call in.useDelimiter("[^AZa-z0-9_]+"). Then each call to next returns an identifier. Java. Explain logic used lease.
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 Java program that reads words from a text file and displays all the non-duplicate...
Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra
1)  Write a python program that opens a file, reads all of the lines into a list...
1)  Write a python program that opens a file, reads all of the lines into a list of strings, and closes the file. Use the Readlines() method. Test your programing using the names.txt file provided. 2) Convert the program into a function called loadFile, that receives the file name as a parameter and returns a list of strings. 3) Write a main routine that calls loadFIle three times to load the three data files given into three lists. Then choose a...
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...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java 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 output file should...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java 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 output file should...
In java, Write a program that reads a data file containing final exam scores for a...
In java, Write a program that reads a data file containing final exam scores for a class and determines the number of passing scores (those greater than 60) and the number of failing scores (those less than 60). The average score as well as the range of scores should also be determined. The program should request the name of the data file from the end user. The file may contain any number of scores. Using a loop, read and process...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT