Concept explainers
Define a class called Counter. An object of this class is used to count things, so it records a count that is a nonnegative whole number. Include methods to set the counter to 0, to increase the count by 1, and to decrease the count by 1. Be sure that no method allows the value of the counter to become negative. Also include an accessor method that returns the current count value, as well as a method that displays the count on the screen. Do not define an input method. The only method that can set the counter is the one that sets it to zero. Write a
Trending nowThis is a popular solution!
Chapter 5 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
Additional Engineering Textbook Solutions
Starting Out With Visual Basic (8th Edition)
Absolute Java (6th Edition)
Problem Solving with C++ (9th Edition)
Starting Out with Python (4th Edition)
Problem Solving with C++ (10th Edition)
Software Engineering (10th Edition)
- Write a class called Investment with fields called principal and interest. The constructor should set the values of those fields. There should be a method called value_after that returns the value of the investment after n years. The formula for this is p(1+i)ⁿ, where p is the principal, and i is the interest rate.arrow_forwardIn class Song, write the calculation in method getEstEarnings of class Song to evaluate estimated earnings. Note that while in the real world, other methods would be used to estimate earnings, in this simplified model, the estimated earnings will be the length of the title, multiplied by the a value extracted by the ministry using the method getSongPartEst(hint.. the singer keeps a reference to the ministry. You can observe how the reference is used in method getClaimableEarnings). class Song { private String title, genre; Singer singer; private Studio studio; public Song(String title, String genre,Singer singer int, length) { this.studio = null; this.genre = genre; this.singer = singer; this.title = title; } public int getEstEarnings() { return 0;//Q2. Perform this calculation } public int getClaimableEarnings() { int est = getEstEarnings(); if (est >…arrow_forwardWrite a java program in a class CountPoor that counts the number of families that are considered poor. Write and use a class Family that has the attributes. income—a double value that is the income for the family size—the number of people in the family and the following methods: Family(income, size)—a constructor that sets the attributes isPoor(housingCost, foodCost)—a method that returns true if housingCost + foodCost * size is greater than half the family income (foodCost is the average food cost for an individual, while housingCost is for the family) toString—a method that returns a string containing the information about the family The program should read an integer k from the keyboard and then create an array of size k whose base type is Family. It should then create k objects of type Family and put them in the array, reading the income…arrow_forward
- Write a Java class called Power. This class defines a method called power() which takes as parameters a double called base as well as an int called exponent, and returns a double representing the value of base raised to the power exponent. The exponent is a non-negative integer value, and that base and exponent are not both 0. You MUST write the computation yourself; in other words, you MUST NOT use the Math.pow() method. Repeat the operation often.arrow_forwardIn Java As discussed in the class, overloaded methods are two or more methods in the sameclass that use the same name but differ in the signature. Following are headings forthree overloaded findMinimum methods.int findMinimum(int a, int b, int c)double findMinimum(double a, double b, double c)double findMinimum(double a, double b, double c, double d).These methods should return the given arguments' minimum (smallest) value. Yourtask is to implement these static overloaded methods.public class Minimum{ public static void main(String args []) { int oneI = 50; // first number int twoI =14; // second number int threeI =18; // third number double oneD = 44.0; // first number double twoD =14.0; // second number double threeD =18.0; // third number double fourD =0.0; System.out.println("The minimum of" + oneI + ","+ twoI + "," + "and" +threeI + "is:" + findMinimum(oneI,twoI,threeI); System.out.println("The minimum of" + oneD + ","+ twoD + "," + "and" +threeD +"is:" + +…arrow_forwardCould you please help me with this Exercise? The language is JAVA. Thank You! Exercise 11.4 A “rational number” is a number that can be represented asthe ratio of two integers. For example, 2/3 is a rational number, and you canthink of 7 as a rational number with an implicit 1 in the denominator. The purpose of this exercise is to write a class definition that includes a va-riety of methods, including constructors, static methods, instance methods, modifiers, and pure methods:1. Define a class called Rational. A Rational object should have twointeger instance variables that store the numerator and denominator.2. Write a constructor that takes no arguments and sets the numerator to0 and denominator to 1.3. Write an instance method called printRational that displays a Rationalobject in a reasonable format. 11.10 Exercises 199 4. Write a main method that creates a new object with type Rational,sets its instance variables to the values of your choice, and displays theobject.5. You now have…arrow_forward
- Could you tell me how to fix the error in testing the following code: class Employee:""" The employee class has a constructor and getter methods.The constructor has input arguments"""# Assigns values for id, name, salary, and email from input argumentsdef __init__(self, i, n, s, e):self.id = iself.name = nself.sal = sself.email = e# getter function for namedef get_name(self):return self.name# getter function for IDdef get_id(self):return self.id# getter function for salarydef get_sal(self):return self.sal# getter function for emaildef get_email(self):return self.emaildef make_employee_dict(ids, names, sal, emails):"""Function to create employee object and the dictionaryusing those objects"""# k is the length of all the listsk = len(ids)emp_dict = dict()# Making employee object and adding to dictionaryfor i in range(k):e = Employee(ids[i], names[i], sal[i], emails[i])emp_dict[ids[i]] = e# return the dictionaryreturn emp_dict# Testingemp_names = ["Jean", "Kat", "Pomona"]emp_ids = ["100",…arrow_forwardWrite a program that simulates the Magic 8 Ball game. The class should have a method that returns an answer at random. Test your class by outputting the answer several times.arrow_forwardWrite a program that calls methods for each of the following mini problems from its main method. Us a single Scanner for the program declared in the class asprivate Static Scanner keyboard = new Scanner(System.in); You have three identical prizes to give away and a pool of 10 finalists. The finalists are assigned numbers from 1 to 10. Write a method to randomly select the numbers of 3 finalists to receive a prize. Make sure not to pick the same number twice. For example, picking finalists 3, 6, 2 would be valid but picking 3, 3, 11 would be invalid because finalist number 3 is listed twice and 11 is not a valid finalist number. For this problem you can insert the following line of code to generate a random number between 1 and 10:int num = (int) (Math.random() * 10) +1; Notes: This project uses random numbers in a loop. You might wish to introduce the Random class instead of Math.random(). 2. Suppose we can buy a chocolate bar from the vending machine for $1 each. Inside…arrow_forward
- Write a program that calls methods for each of the following mini problems from its main method. Us a single Scanner for the program declared in the class asprivate Static Scanner keyboard = new Scanner(System.in); Suppose we can buy a chocolate bar from the vending machine for $1 each. Inside every chocolate bar is a coupon. We can redeem 6 coupons for one chocolate bar from the machine. This means that once you have started buying chocolate bars from the machine, you always have some coupons. We would like to know how many chocolate bars can be eaten if we start with N dollars and always redeem coupons if we have enough for an additional chocolate bar.For example, with 6 dollars we could consume 7 chocolate bars after purchasing 6 bars giving us 6 coupons and then redeeming the 6 coupons for one bar. This would leave us with one extra coupon. For 11 dollars, we could have consumed 13 chocolate bars and still have one coupon left. For 12 dollars, we could have consumed 14…arrow_forwardTo rectify the problem identified in the following program,add a public method called isZero() to Fraction class. This method will determine if a Fraction represents a zero fraction. A zero fraction has a numerator == 0, no matter what the denominator is. isZero() method should return a Boolean result indicating a zero fraction or otherwise. The method will be used by the client class (TestFraction.java) to test whether the ‘calling fraction’ is equal to the number zero. Modify old client class so that it now loops until a fraction representing zero is entered. //TestFraction.javaimport java.util.Scanner;//test programpublic class TestFraction{ public static void main(String[] args) { //create a Scanner object Scanner in = new Scanner(System.in); //create new fraction Fraction frac = new Fraction(); //declare numerator and denominator int n=1,d; System.out.print("Enter Fraction's Numerator: "); //input numerator n =…arrow_forwardIn this lab you will write three classes: Die, PairOfDice, and Player.The Die class mimics the rolling of a die. Specifically, this class will implement the following methods:• A default constructor initializing the number of sides of a die to 6.• An overloaded constructor that takes an integer number of sides (assume greater than 1).• roll which generates and returns a random number between 1 and the number of sides(inclusive).• An accessor method to read the value of the face on the die.• A toString method returning the string representation of the face value.The maximum number of sides should be stored as a private constant in the Die class. Also use theRandom class for the random number generator.The PairOfDice class mimics the rolling of two dice. Specifically, this class will implement thefollowing methods:• A default constructor that creates and initializes the number of sides of each die to 6.• An overloaded constructor that creates and takes two integer number of sides, one…arrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT