Current Code: public class Briefcase { private double $value; public boolean cho
ID: 3826957 • Letter: C
Question
Current Code:
public class Briefcase {
private double $value;
public boolean chosen;
public double getValue(){
return $value;
}
public void setValue(double $value){
this.$value =$value;
chosen = false;
}
public boolean isChosen(){
return chosen;
}
public void setChosen(boolean chosen){
this.chosen = chosen;
chosen = true;
}
}
import java.io.*;
import java.util.*;
public class BriefcaseCollection {
private Briefcase[] cases;
public BriefcaseCollection(double[] amounts){
cases = new Briefcase[amounts.length];
for (int k = 0; k < cases.length; ++k ){
Briefcase briefcase = new Briefcase(amounts[k]);
cases[k] = briefcase;
}
}
public double getLastCase(int index){
return cases[index].getLastCase();
}
public double showRemainingCases(){
}
public double chooseThisCase(){
}
public double invalidChoice(){
}
public double computerAverage(){
}
private double permute(){
}
}
Briefcase This class represents a single briefcase Fields Svalue The dollar value contained in the briefcase, double true if the player has chosen this briefcase: false otherwise chosen Methods Constructor. Takes aparameter toinitialize the data field Svalue Briefcase get Value() Accessor. Returns Svalue set ValueO Mutator. Takes a parameter Assigns Svalue the paramete isChosenO Returns the value of the instance field,chosen accessor) no setChosen Mutator Takes a parameter. Assigns field chosen the parameter BriefcaseCollection This class represents as an array the collection of l 1 briefcases used in the game Fields An array of type Briefcase. The array stores the collection of briefcases of cases the game Methods BriefcaseCollection() Constructor. Takes an array of double values for parameter (this parameter is intended to initialize the dollar values in the briefcases of the collection, see below). Instantiates and initializes the array field cases to the length of the parameter array. Runs a for loop and instantiates the Briefcase array entries, such that it initializes $value of Briefcase of index k to the parameter array entry of the same index. Calls the permute() method to create a random re-arrangement of the briefcases in the arra get LastCaseO Returns the last remaining case after all others have been chosen. howRemainingCases() Displays as a list the serial numbers of those array entries from cases which have not been chosen. The serial number is 1 greater than the array index of the same briefcase For instance, if only the briefcases of index 0, 1,4, 8 have not yet been hosen, the output would be: l 2 5 9 choose ThisCase() Takes a parameter say, num of type int, to be used as the serial number of a briefcase. The method sets the chosen field of the Briefcase of index num-1 to true and returns the corresponding Briefcase invalid Choice() Takes a parameter say, num of type int, to be used as the serial number of a briefcase. The method returns true if num is an invalid choice that is, if it is less than 1 or greater than cases.length or the case of array index num -1 has been chosen (use the isChosen method of the Briefcase class computeAverage() Computes and returns the average of the dollar values currently in the collection (briefcases not chosen considered onl Private helper method, void. Creates a random permutation of the elements permute of cases, the method runs a for loop to the length of the array, in the loop for every loop index kanother array index qP0 is randomly selected then cases Ikl is swapped with casesIml, where m (k )%cases lengthExplanation / Answer
Assumption: - I assume that getLastCase() function will only be called when all except one case is chosen.
Below is you code. I have added the comments for your understanding. I have also added one driver class with main function to demonstrate the project to be working. I have provided with sample outputs also: -
BriefcaseDriver.java
import java.util.Random;
public class BriefcaseDriver {
public static void main(String[] args) {
double[] amount = {2,3.4,5,6.7,1,4.5,2.04,6,23,10,22};
BriefcaseCollection brfColl = new BriefcaseCollection(amount); //creating a briefcase array
// Printing initial details about the collection before choosing briefcases.
System.out.println("Initial Cases:"+brfColl.showRemainingCases());
System.out.println("Initial Average before choosing:"+brfColl.computerAverage());
//Randomly choosing briefcases out of half of the collection
int q,m;
Briefcase cse;
for (int i = 0; i < amount.length/2; i++) {
q = new Random().nextInt(amount.length - 1) + 1;
m = (i + q) % amount.length;
if(!brfColl.invalidChoice(m)) { // checking validity of the briefcase.
cse =brfColl.chooseThisCase(m);
System.out.println("Briefcase #"+(m)+" which is choosen has value:"+cse.getValue());
} else {
System.out.println("Briefcase #"+(m)+" chosen is invalid or already chosen.");
}
}
// showing the remaining collection and average after choosing some briefcases.
System.out.println("Remaining Cases:"+brfColl.showRemainingCases());
System.out.println("Final Average after choosing:"+brfColl.computerAverage());
}
}
BriefcaseCollection.java
import java.util.*;
public class BriefcaseCollection {
private Briefcase[] cases;
public BriefcaseCollection(double[] amounts) {
cases = new Briefcase[amounts.length];
for (int k = 0; k < cases.length; ++k) {
Briefcase briefcase = new Briefcase(amounts[k]);
cases[k] = briefcase;
}
permute();
}
public Briefcase getLastCase() {
for (int i = 0; i < cases.length; i++) {
if (!this.cases[i].isChosen()) {
return this.cases[i];
}
}
return new Briefcase();
}
public String showRemainingCases() {
String s = "";
for (int i = 0; i < this.cases.length; i++) {
if (!cases[i].chosen) {
s = s + " " + (i + 1);
}
}
return s;
}
public Briefcase chooseThisCase(int num) {
Briefcase cas = this.cases[num - 1];
cas.chosen = true;
return cas;
}
public boolean invalidChoice(int num) {
if (num < 1 || num > this.cases.length || this.cases[num - 1].isChosen()) {
return true;
}
return false;
}
public double computerAverage() {
double sum = 0;
int count = 0;
for (int i = 0; i < this.cases.length; i++) {
if (!this.cases[i].isChosen()) {
sum = sum + this.cases[i].getValue();
count++;
}
}
return sum / count;
}
private void permute() {
Briefcase temp;
int q, m;
for (int i = 0; i < this.cases.length; i++) {
q = new Random().nextInt(this.cases.length - 1) + 1;
m = (i + q) % this.cases.length;
temp = this.cases[i];
this.cases[i] = this.cases[m];
this.cases[m] = temp;
}
}
}
Briefcase.java
public class Briefcase {
private double $value;
public boolean chosen;
public Briefcase(double d) {
this.$value = d;
this.chosen = false; // Default briefcase is not choosen
}
public Briefcase() {
this.$value = 0;
this.chosen = false;
}
public double getValue() {
return $value;
}
public void setValue(double $value) {
this.$value = $value;
}
public boolean isChosen() {
return chosen;
}
public void setChosen(boolean chosen) {
this.chosen = chosen;
}
}
Sample Runs: -
Output 1
Initial Cases: 1 2 3 4 5 6 7 8 9 10 11
Initial Average before choosing:7.785454545454545
Briefcase #8 which is choosen has value:6.7
Briefcase #6 which is choosen has value:23.0
Briefcase #8 chosen is invalid or already chosen.
Briefcase #7 which is choosen has value:10.0
Briefcase #0 chosen is invalid or already chosen.
Remaining Cases: 1 2 3 4 5 9 10 11
Final Average after choosing:5.7425
Output 2
Initial Cases: 1 2 3 4 5 6 7 8 9 10 11
Initial Average before choosing:7.785454545454547
Briefcase #6 which is choosen has value:3.4
Briefcase #3 which is choosen has value:10.0
Briefcase #1 which is choosen has value:22.0
Briefcase #1 chosen is invalid or already chosen.
Briefcase #2 which is choosen has value:4.5
Remaining Cases: 4 5 7 8 9 10 11
Final Average after choosing:6.534285714285715
Output 3
Initial Cases: 1 2 3 4 5 6 7 8 9 10 11
Initial Average before choosing:7.785454545454547
Briefcase #10 which is choosen has value:3.4
Briefcase #4 which is choosen has value:22.0
Briefcase #8 which is choosen has value:23.0
Briefcase #0 chosen is invalid or already chosen.
Briefcase #9 which is choosen has value:1.0
Remaining Cases: 1 2 3 5 6 7 11
Final Average after choosing:5.177142857142856
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.