Question

Write a complete Java program to create three types of counters as follows: The first counter...

Write a complete Java program to create three types of counters as follows:

  1. The first counter increases its value by one.
  2. The second counter increases its value by two.
  3. The third counter increases its value by three.

Please follow these notes:

  1. Create one class for all counter types. The name of the class is Three_type_counter.
  2. Define a constructor method that initial values for all counters to 7 when the class object created.
  3. Create method for each counter as: count1, count2 and count3. Each method has to increase each counter value by one, two and three respectively.
  4. In the main program:
    1. Create new object with name “obj_counter”.
    2. Call the three methods for each counter at one time.
    3. Print out the values for all counters.(support your answer by screenshot)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

class Three_type_counter
{
    int count1=0;// counter varible for first counter
    int count2=0;;// counter varible for 2nd counter
    int count3=0;;// counter varible for 3rd counter
    Three_type_counter()
    {
        //constructor method that initial values for all counters to 7 when the class object created
        count1=7;
        count2=7;
        count3=7;
    }
    //method 1 counter
    void count1()
    {
        this.count1=this.count1+1;
    }
    //method 2 counter
    void count2()
    {
        this.count2=this.count2+2;
    }
    // method 3 counter
    void count3()
    {
        this.count3=this.count3+3;
    }
  
}
public class Main
{
   public static void main(String[] args) {
        // creating object for the Three_type_counter class
       Three_type_counter obj_counter=new Three_type_counter();
       // calling methods
       obj_counter.count1();
       obj_counter.count2();
       obj_counter.count3();
       // printing counter values
       System.out.println("counter1 :"+obj_counter.count1);
       System.out.println("counter2 :"+obj_counter.count2);
       System.out.println("counter3 :"+obj_counter.count3);
   }
}

CODE SCREENSHOT:

1 class Three_type_counter 2 { int count1=0;// counter varible for first counter int count2=0;;// counter varible for 2nd cou
30 public class Main 31{ public static void main(String [] args) { //creating object for the Three_type_counter class Three_t

OUTPUT:

counterl :8 counter2 9 counter3 10

Add a comment
Know the answer?
Add Answer to:
Write a complete Java program to create three types of counters as follows: The first counter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

  • it's a JAVA program. Share Edit & Create aanmethod that tests all three overloaded methods. Save...

    it's a JAVA program. Share Edit & Create aanmethod that tests all three overloaded methods. Save the application as Billing.java. a. Create a FitnessTracker class that includes data fields for a fitness activity the number of minutes spent participating, and the date. The class includes methods to get each field. In addition, create a default constructor that automatically sets the activity to running, the minutes to 0, and the date to January 1 of the current year. Save the file...

  • JAVA basic quiz. 1. a) In order to run a java program, the computer must first...

    JAVA basic quiz. 1. a) In order to run a java program, the computer must first compile the .java files to create .class files. Then the Java Virtual Machine (JVM) runs the .class files. After compilation, when the JVM first starts to run the program, what is created in memory first? When do instances of an object get created? How long do they stay in memory? Under what circumstances will the JVM delete an object from memory? b)What are the...

  • JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class...

    JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare (); } The prepare method will...

  • Java Programming assignment. 1. Create a class called Square that takes a width parameter in the...

    Java Programming assignment. 1. Create a class called Square that takes a width parameter in the constructor. The Square class should have a draw() method that will draw the square on the screen. Create a class called TestSquare that will take width from the user, create an object of Square, and invoke the draw() method on the square object. Below is a UML diagram for the Square and Rectangle class: 3. Create a zip file that contains your Java programs....

  • Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named...

    Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type(does not mention the data type of the variable coffeeType), price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • WRITE JAVA PROGRAM Problem Statement You are required to write a stock price simulator, which simulates...

    WRITE JAVA PROGRAM Problem Statement You are required to write a stock price simulator, which simulates a price change of a stock. When the program runs, it should do the following:  Create a Stock class which must include fields: name, symbol, currentPrice, nextPrice, priceChange, and priceChangePercentage.  The Stock class must have two constructor: a no-argument constructor and a constructor with four parameters that correspond to the four fields. o For the no-argument constructor, set the default value for...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a...

    Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a float Speed and string Name, an abstract method Drive, and include a constructor. Create a Tesla class - Tesla inherits from Vehicle. - Tesla has an int Capacity. - Include a constructor with the variables, and use super to set the parent class members. - Override Drive to increase the speed by 1 each time. - Add a toString to print the name, speed,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT