Question

In: Computer Science

Problem 01: GroceryCalculator Write a program to calculate the cost of a list of grocery items....

Problem 01: GroceryCalculator

Write a program to calculate the cost of a list of grocery items. It includes one attribute: totalCost. It includes five user-defined methods:

  • getItemName which asks the user to type an item name and returns the name of the item. It returns a string.
  • getItemPrice which asks the user to type the price for an item and returns the price, which return a double.
  • getItemQuantity which asks the user to type how many of the item they want to buy and returns an integer variable.
  • getTotalGrocery, which calls the above three methods. Inside this method, use a while loop to allow the user to enter more items. The user may type "q" to quit typing more items. In this method, calculate the total cost of all the grocery items and save the result to the class attribute totalCost,
  • getTotalCost, which return the total cost of all the grocery items.
  • The main method calls two methods: getTotalGrocery and getTotalCost. In this method, print the total cost.

Problem 02: Dog

  • Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes.
  • Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish.
  • Define a getter and a setter for each attribute.
  • Define a method toString to return a String type, the returned string should have this information:

“Hi, my name is Lucky. I am a Chiwawa. I am 3 years old, and my color is white.”

  • Create a main method. In the main method, create a Dog type object and call setters to set its attributes. Print this object using System.out.println().

Solutions

Expert Solution

PROGRAM -

import java.util.*;
import java.io.*;

public class store{
    double TotalCost;

    //asks the user to type an item name and returns the name of the item. It returns a string.
    public String getItemName(){
        System.out.println("Enter the item name: ");
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        return name;
    }
    //asks the user to type the price for an item and returns the price, which return a double.
    public double getItemPrice(){
        System.out.println("Enter the item price: ");
        Scanner sc = new Scanner(System.in);
        double price = sc.nextDouble();
        return price;
    }
    //asks the user to type how many of the item they want to buy and returns an integer variable.
    public int getItemQuantity(){
        System.out.println("Enter the item quantity: ");
        Scanner sc = new Scanner(System.in);
        int Quantity = sc.nextInt();
        return Quantity;
    }

    //calls the above three methods. Inside this method, use a while loop to allow the user to enter more items. The user may type "q" to quit typing more items. In this method,
    public void getTotalGrocery()
    {
        double cost = 0;
        while(true)
        {
            String name= getItemName();
            double price = getItemPrice();
            int Quantity = getItemQuantity();
            cost+=price*Quantity;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter anything to continue, press q to exit.... ");
            String s = sc.next();
            if(s.equals("q"))
            {
                break;
            }

        }
        TotalCost = cost;
    }

    //returns the total cost
    public double getTotalCost(){
        return TotalCost;
    }

    public static void main(String[] args) {

        store s = new store();//creating a new object
        s.getTotalGrocery();//calling to get all items
        System.out.println("The total cost is: "+s.getTotalCost());//print result
    }
}

INTENDATION-

OUTPUT TESTED-

ASK ANY DOUBTS IN THE COMMENTS.


Related Solutions

Grocery List Program Write a program that keeps track of the user's grocery list items. Prompt...
Grocery List Program Write a program that keeps track of the user's grocery list items. Prompt the user if they'd like to (each action is a function): See the list Display all items (if any) in the list Add item to their list Confirm with the user before adding item (y/n or yes/no) If they enter a duplicate item, notify them the item already exists Remove items from their list Confirm with the user before removing item (y/n or yes/no)...
Given a list of items, write a program that generates a list of lists of the...
Given a list of items, write a program that generates a list of lists of the following form: [a,b,c,...,z]⇒[[z], [y,z], [x,y,z], ... , [a,b, ... ,y,z]] Hint: Slicing is your friend. please write a python program
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that...
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that asks for the name of the item and the price of each item, and then displays these on the screen. Refer to the print() and input() statements covered in the module. Do NOT use loops as it will be covered in later modules. Apples 2.10 Hamburger 3.25 Milk 3.49 Sugar 1.99 Bread 1.76 Deli Turkey 7.99 Pickles 3.42 Butter 2.79
A customer comes into a grocery store and buys 8 items.   Write a PYTHON program that...
A customer comes into a grocery store and buys 8 items.   Write a PYTHON program that prompts the user for the name of the item AND the price of each item, and then simply displays whatever the user typed in on the screen nicely formatted.
Write the Pseudocode for the following programming problem. Write a program that will calculate a tip...
Write the Pseudocode for the following programming problem. Write a program that will calculate a tip based on the meal price and a 6% tax on a meal price. The user will enter the meal price and the program will calculate tip, tax, and the total. The total is the meal price plus the tip plus the tax. Your program will then display the values of tip, tax, and total. The tip amounts based on the mean price are as...
In this problem, you will write a program that reverses a linked list. Your program should...
In this problem, you will write a program that reverses a linked list. Your program should take as input a space-separated list of integers representing the original list, and output a space-separated list of integers representing the reversed list. Your algorithm must have a worst-case run- time in O(n) and a worst-case space complexity of O(1) beyond the input. For example, if our input is: 5 7 1 2 3 then we should print: 3 2 1 7 5 Please...
Write a program that will calculate the cost of installing fiber optic cable at a cost...
Write a program that will calculate the cost of installing fiber optic cable at a cost of .87 per ft for a company. Your program should display the company name and the total cost. Display a welcome message for your program. Get the company name from the user Get the number of feet of fiber optic to be installed from the user Multiply the total cost as the number of feet times .87. Display the calculated information and company name.
Write a program in C++ that solves this problem Calculate the area and volume of a...
Write a program in C++ that solves this problem Calculate the area and volume of a sphere problem. Inside a for loop, vary the radius from 10 to 40  with a step or increment of 5 and calculate the area and volume Your radius will be equal to your loop counter. All calculations should have 2 decimal places, but the radius should have zero decimal places and any number of 1,000 or more should have a comma. Print the radius, area,...
Write a program that will calculate and display the cost of a gasoline purchase based on...
Write a program that will calculate and display the cost of a gasoline purchase based on the brand of gasoline, the octane rating and the amount purchased. Your program should first display a menu prompting the user for the brand of gasoline.  Next prompt the user for the amount of gasoline in liters. Finally, your program should calculate and display the cost of the gasoline purchase using the formula below. Cost of Discount Gasoline = ($2.49 - (100 - Octane Rating)...
Read in the names of several grocery items from the keyboard and create a shopping list...
Read in the names of several grocery items from the keyboard and create a shopping list using the Java ArrayList abstract data type. Flow of Program: 1) Create a new empty ArrayList 2) Ask the user for 5 items to add to a shopping list and add them to the ArrayList (get from user via the keyboard). 3) Prompt the user for an item to search for in the list. Output a message to the user letting them know whether...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT