Question

In: Computer Science

Write 2 short Java programs based on the description below. 1) Write a public Java class...

Write 2 short Java programs based on the description below.

1) Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor.

The string should contain the following words:
{“the”, “quick”, “brown”, “fox”}

2) Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number.

For example, 60 seconds:
00:00
00:01
00:02
. . .
00:58
00:59
01:00

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

// PROGRAM 1
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class TextFileEditor {

    final private String fileName;

    public TextFileEditor() {
        this.fileName = "words.dat";
    }

    public void writeToFile(String[] words) throws IOException {

        FileWriter writer = new FileWriter(new File(fileName));
        if (words == null || words.length == 0) throw new IllegalArgumentException("String array empty or null.");

        for (String word : words) {
            writer.write(word + "\r\n");
        }
        writer.flush();
        writer.close();

        System.out.println("File: " + fileName + " updated sucessfully.");

    }

    public static void main(String[] args) {

        String words[] = {"the", "quick", "brown", "fox"};

        TextFileEditor editor = new TextFileEditor();
        try {
            editor.writeToFile(words);
        } catch (IOException e) {
            System.out.println("Error: Could not write data to file.");
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }

    }
}

=======================================================================

// PROGRAM 2

public class DecimalTimer {

    private int seconds;

    public DecimalTimer() {
        this.seconds = 0;
    }

    public void printCounter() {

        int minute = seconds / 60;
        int second = seconds % 60;

        if (minute < 10) System.out.print("0");
        System.out.print(minute + ":");
        if (second < 10) System.out.print("0");
        System.out.println(second);
        this.seconds += 1;

    }

    public static void main(String[] args) {

        DecimalTimer timer = new DecimalTimer();
        for (int tick=1; tick<=62; tick++){
            timer.printCounter();
        }
    }
}

=======================================================================


Related Solutions

Task 2/2: Java program Based upon the following code: public class Main {   public static void...
Task 2/2: Java program Based upon the following code: public class Main {   public static void main( String[] args ) {     String alphabet = "ABCDEFGHIJKLMNMLKJIHGFEDCBA";     for( <TODO> ; <TODO> ; <TODO> ) {       <TODO>;     } // Closing for loop   } // Closing main() } // Closing class main() Write an appropriate loop definition and in-loop behavior to determine if the alphabet string is a palindrome or not. A palindrome is defined as a string (or more generally, a token) which...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a  ...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to   be   thrown   when   a   string   is   discovered   that   has   too   many   characters   in   it. Consider   a   program   that   takes   in   the   last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,  ...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
The goal of this assignment is to write five short Java/Pyhton programs to gain practice with...
The goal of this assignment is to write five short Java/Pyhton programs to gain practice with expressions, loops and conditionals. Boolean and integer variables and Conditionals. Write a program Ordered.java that reads in three integer command-line arguments, x, y, and z.   Define a boolean variable isOrdered whose value is true if the three values are either in strictly ascending order  (x < y < z) or in strictly descending order  (x > y > z), and false otherwise. Print out the variable isOrdered...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content)...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content) { // 3           this.content = content.toUpperCase(); // 4      } // 5      public String toString() { // 6           return content.toUpperCase(); // 7      } // 8      public static void main(String[] args) { // 9           UpperCaseString upperString =              new UpperCaseString("Hello, Cleo!"); // 10           System.out.println(upperString); // 11      } // 12 } // 13 THE FOLLOWING 3 QUESTIONS REFER...
For each problem below, write a java program to solve it, name your programs as PA2_1.java...
For each problem below, write a java program to solve it, name your programs as PA2_1.java and PA2_2.java. 1. We have a steam heating boiler whose bursting pressure is known, but we of course want to use it only at pressures well below this limit. Our engineers recommend a safety factor of 3; that is, we should never exceed a pressure that is one-third of the rated bursting pressure. Write a java program that reads in the rated bursting pressure...
write the following programs: Ram.java, Truck.java, LandVehicle.java and Vehicle.java. public class Ram extends Truck { public...
write the following programs: Ram.java, Truck.java, LandVehicle.java and Vehicle.java. public class Ram extends Truck { public static void main(String[] args) { // your implementation } public Ram() { // your implementation } } public class Truck extends LandVehicle { public Truck() { // your implementation } public Truck(String s) { // your implementation } } public class LandVehicle extends Vehicle { public LandVehicle() { // your implementation } } public class Vehicle { public Vehicle() { // your implementation
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT