Question

Define the Bag class in the file Bag.java with the following structure. Define the public methods...

Define the Bag class in the file Bag.java with the following structure. Define the public methods listed, including the constructors. You may add fields to the class as necessary.

// A class representing a bag of items

public class Bag {

}

// Construct a bag, initially empty, with the indicated weight that it can hold (in ounces)

public Bag ( int weight ) { ... }

// Construct a bag, initially empty, with an assumed weight of 20 ounces.

public Bag ( ) { ... }

// return the current weight of items in the bag

public int currentWeight ( ) { ... }

// put moreOunces into the bag – but never more than the weight that it can hold.

public void addItems ( int moreOunces ) { ... }

// remove amount ounces from the bag (or as many as possible when not enough is present) // return how many were actually removed.
public int removeItems ( int amount ) { ... }

Example Uses of Bag

Below is the file UseBag.java, showing usage of the Bag class. Your code should compile/run with this code.

public class UseBag {
public static void main(String[] args){

Bag b = new Bag(10); System.out.println(b.currentWeight());

b.addItems(3);

System.out.println(b.currentWeight());

b.addItems(50);

System.out.println(b.currentWeight());

int got = b.removeItems(8);

System.out.println("removed "+got+" ounces");

System.out.println(b.currentWeight());

got = b.removeItems(5);

System.out.println("removed "+got+" ounces"); System.out.println(b.currentWeight());

Bag b2 = new Bag();
b2.addItems(30); System.out.println(b2.currentWeight());

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Following is the answer:

Bag.java

package Bag;

public class Bag {
    private int weight;
    public int ounces = 0;

    //constructor with parameter weight
    public Bag(int weight){
        this.weight = weight;
    }

    //Default constructor that assumes the weight 20
    public Bag(){
        this.weight = 20;
    }

    //addItems method that add the ounces 
    public void addItems(int moreOunces){
        //if ounces less than weight the add it else add remaining ounces (substract from weight and find how many are remaining)
        if(moreOunces <= weight ){
            ounces+=moreOunces;
        }else{
            ounces+=weight-ounces;
        }
    }

    //removes items from if amount is greater than ounces available then removes all the ounces
    public int removeItems(int amount){
        int a;
        if(amount >= ounces){
            ounces=0;
        }else{
            ounces=ounces-amount;
        }
        return amount;
    }

    public int currentWeight() {
        return ounces;
    }
}

UseBag.java

package Bag;

public class UseBag {
    public static void main(String[] args){

        Bag b = new Bag(10); System.out.println(b.currentWeight());

        b.addItems(3);

        System.out.println(b.currentWeight());

        b.addItems(50);

        System.out.println(b.currentWeight());

        int got = b.removeItems(8);

        System.out.println("removed "+got+" ounces");

        System.out.println(b.currentWeight());

        got = b.removeItems(5);

        System.out.println("removed "+got+" ounces"); System.out.println(b.currentWeight());

        Bag b2 = new Bag();
        b2.addItems(30);
        System.out.println(b2.currentWeight());

    }
}

output:

10 removed 8 ounces removed 5 ounces 20

Add a comment
Know the answer?
Add Answer to:
Define the Bag class in the file Bag.java with the following structure. Define the public methods...
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
  • Given code: /** * Provides some methods to manipulate text * */ public class LoopyText {...

    Given code: /** * Provides some methods to manipulate text * */ public class LoopyText { private String text;    /** * Creates a LoopyText object with the given text * @param theText the text for this LoopyText */ public LoopyText(String theText) { text = theText; }    //Your methods here } Given tester code: /** * Tests the methods of LoopyText. * @author Kathleen O'Brien */ public class LoopyTextTester { public static void main(String[] args) { LoopyText loopy =...

  • given the following two classes Within a file named Rational.java, define a public class named Rational...

    given the following two classes Within a file named Rational.java, define a public class named Rational such that … the Rational class has two private instance variables, numerator and denominator, both of type int the Rational class defines two public accessor methods, numerator() and denominator() the Rational class defines a constructor that has one String parameter a throws clause indicates that this constructor could potentially throw a MalformedRationalException this constructor throws a MalformedRationalException in the following circumstances … When the...

  • [JAVA] help —————— ListArrayMain.java ——————- public class ListArrayMain { public static void main (String[] args) {...

    [JAVA] help —————— ListArrayMain.java ——————- public class ListArrayMain { public static void main (String[] args) { ListArray L1 = new ListArray(); // constructs the list L1 L1.InsertBegin(2); L1.InsertBegin(3); L1.InsertBegin(7); L1.InsertBegin(15); L1.InsertBegin(5); L1.InsertEnd(666); L1.InsertAfter(1000,7); // Now, let's print out the array to verify the insertions worked. System.out.println("The items in the list are:"); for (int i = 0; i<= L1.lastCell; i++) { System.out.println(L1.a[i]); } System.out.println("The last cell number is: " + L1.lastCell); }// end main } ———————————- ListArray.java ———————————— public class ListArray...

  • Analyze the following code: public class Test { public int x; public Test(String t) { System.out.println("Test");...

    Analyze the following code: public class Test { public int x; public Test(String t) { System.out.println("Test"); public static void main(String[] args) { Test test: System.out.println(test.x); The program has a compile error because Test class does not have a default constructor The program has a compile error because test is not initialized OO The program has a compile error because x has not been initialized The program has a runtime NullPointerException while executing test.x because test is a null reference and...

  • A general shape class is shown below. Shape has a dimension. It also defines constructors, getters,...

    A general shape class is shown below. Shape has a dimension. It also defines constructors, getters, setters and a toString method. class Shape{ int dimension; public Shape(){} public Shape(int newDimension){ dimension = newDimension; } public int getDimension(){ return dimension; } public void setDimension(int newDimension){ dimension = newDimension; } public String toString(){ return "Shape has dimension "+dimension; } } a. Define classes Circle and Square, which inherit from Shape class. b. Both classes must have two constructors similar to Shape class....

  • Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name...

    Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name Access modifier: private Data Type: String Name: currentWeight Access modifier: private Data Type: double Name: maximumWeight Access modifier: private Data Type: double Constructors Name: Bag Access modifier: public Parameters: none (default constructor) Task: sets the value of the instance variable name to the empty string sets the value of the instance variable currentWeight to 0.0 sets the value of the instance variable maximumWeight to...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {   super();   System.out.println(“B() called”); } public...

  • Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQ...

    Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQueue { private final int INITIAL_SIZE = 10;   private int [] A = new int[INITIAL_SIZE]; // array to hold queue: front is always at Q[0] // and rear at A[next-1] int next = 0; // location of next available unused slot // interface methods public void enqueue(int key) { //TODO: push the key onto the back of the queue // YOUR CODE HERE! }...

  • The method m() of class B overrides the m() method of class A, true or false?...

    The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...

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