public class Wallet {
private final int MAX = 10;
private int contents[];
private int count;
// constructors
public Wallet(){
contents = new int[MAX];
count = 0;
}
public Wallet(int a[]){
contents = new int[MAX];
for(int i=0; i < a.length;
i++){
contents[i] =
a[i];
}
count = a.length;
}
public String toString(){
StringBuilder sb = new
StringBuilder("Wallet [");
for(int i = 0; i < count;
i++){
sb.append(contents[i]);
if(i < count
- 1)
sb.append(", ");
}
//sb.append(contents[count-1]);
sb.append("]");
return sb.toString();
}
// return sum of contents
public int value(){
int sum = 0;
for(int i = 0; i < count;
i++){
sum +=
contents[i];
}
return sum;
}
// add banknotes at end
public void add(int banknotes){
contents[count++] =
banknotes;
}
// transfer money from donor to reciever
public void transfer(Wallet donor){
for(int i = count, j = 0; i <
donor.count + count; i++){
contents[i] =
donor.contents[j++];
}
for(int i = 0; i < donor.count;
i++){
donor.contents[i] = 0;
}
count += donor.count;
donor.count = 0;
}
// remove banknote from contents
public boolean remove(int banknote){
boolean flag = false;
int i;
for(i = 0; i < count;
i++){
if(contents[i]
== banknote){
contents[i] = -1;
flag = true;
break;
}
}
if(flag){
count--;
while(i <
count){
contents[i] = contents[i+1];
i++;
}
}
return flag;
}
public boolean sameBankNotesSameOrder(Wallet
other){
boolean flag = true;
if(count != other.count){
return
false;
}
for(int i = 0; i < count;
i++){
if(contents[i]
!= other.contents[i]){
flag = false;
break;
}
}
return flag;
}
}
/*************************WalletTester*************************************/
public class WalletTester {
public static void main(String args[]){
// create a new wallet object using
an array
int a[] = {5, 50, 10, 5};
Wallet myWallet = new
Wallet(a);
// print the value of wallet
System.out.println("my wallet
contains: " + myWallet.toString());
System.out.println("\nvalue of my
wallet is: $" + myWallet.value());
// transfer all banknotes from my
wallet to your wallet
Wallet yourWallet = new
Wallet();
yourWallet.add(1);
yourWallet.transfer(myWallet);
System.out.println("\nnow my wallet
contains: " + myWallet.toString());
System.out.println("\nyour wallet
contains: " + yourWallet.toString());
// remove all 5 dollar notes
while(yourWallet.remove(5));
System.out.println("\nyour wallet
with $5s removed is: " + yourWallet.toString());
int b[] = {10, 5, 10};
Wallet tom = new Wallet(b);
int c[] = {10, 5, 10, 1};
Wallet _dick = new Wallet(c);
int d[] = {10, 5, 10};
Wallet harry = new Wallet(d);
System.out.println("\ntom has same
banknotes in same order as _dick: " +
tom.sameBankNotesSameOrder(_dick));
System.out.println("\ntom has same
banknotes in same order as harry: " +
tom.sameBankNotesSameOrder(harry));
}
}
Java Programming help!!! Anthony W. Smith, 2017 Purpose Purpose of this lab is for you to...
Java programming The purpose of this problem is to practice using a generic Urn class. NOTE: Refer to the code for the ArrayStack class from Chapter 12. Use that code as a starting point to create the Urn class, modifying it to remove the methods in the ArrayStack class (push, pop, etc) then add the methods described below. Also change the variable names to reflect the new class. For example the array name should NOT be stack, instead it should...
Task #3 Arrays of Objects 1. Copy the files Song java (see Code Listing 7.1), Compact Disc.java (see Code Listing 7.2) and Classics.txt (see Code Listing 7.3) from the Student Files or as directed by your instructor. Song.java is complete and will not be edited. Classics.txt is the data file that will be used by Compact Disc.java, the file you will be editing. 2. In Compact Disc.java, there are comments indicating where the missing code is to be placed. Declare...
java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...
please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class Chapter9a_FillInTheCode { public static void main(String[] args) { String [][] geo = {{"MD", "NY", "NJ", "MA", "CA", "MI", "OR"}, {"Detroit", "Newark", "Boston", "Seattle"}}; // ------> exercise 9a1 // this code prints the element at row at index 1 and column at index 2 // your code goes here System.out.println("Done exercise 9a1.\n"); // ------> exercise 9a2 // this code prints all the states in the...
Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions Your task is to write a class called Car. Your class should have the following fields and methods: private int position private boolean headlightsOn public Car() - a constructor which initializes position to 0 and headlightsOn to false public Car(int position) - a constructor which initializes position to the passed in value and headlightsOn to false, and it should throw a NegativeNumberException with a...
I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the following constructor and methods: public KWArrayList() public boolean add(E anEntry) public E get(int index) { public E set(int index, E newValue) public E remove(int index) private void reallocate() public int size() public int indexOf(Object item) Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise Provide a constructor...
In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...
/** This class models a tally counter. */ public class Counter { private int value; private int max; public void setLimit(int maximum) { max = maximum; } /** Gets the current value of this counter. @return the current value */ public int getValue() { return value; } /** Advances the value of this counter by 1. */ public void count() { //-----------Start below here. To do: approximate lines of code = 4 // increments value by 1. if value exceeds limit, print "Limit exceeded" and reset value to 0 //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } /** Resets the value of this counter to 0. */ public void reset() { value = 0; } } public class CounterTester { public static void main(String[] args) { Counter c1 = new Counter(); c1.setLimit(100); for (int i = 0; i < 50; i++) { c1.count(); } System.out.println("Tally: " + c1.getValue()); System.out.println("Expected:\nTally: 50"); for (int i = 0; i < 51; i++) { c1.count(); } System.out.println("Tally: " + c1.getValue()); System.out.println("Expected:\nLimit Exceeded"); System.out.println("Tally: 0"); Counter c2 = new Counter(); c2.setLimit(0); for (int i = 0; i < 5; i++) { c2.count(); } System.out.println("Tally: " + c2.getValue()); System.out.println("Expected:\nLimit exceeded\nLimit exceeded\nLimit exceeded\nLimit exceeded\nLimit exceeded"); System.out.println("Tally: 0"); } }
this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...
Can someone help me with the main class of my code. Here is the assignment notes. Implement project assignment �1� at the end of chapter 18 on page 545 in the textbook. Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that the method is delcared static. Therefore, you do not need to create a new instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)" with the appropriate 4 parameter values. When...