Give identifiers semantic meaning and make them easy to read (examples numstuden
ID: 3826167 • Letter: G
Question
Give identifiers semantic meaning and make them easy to read (examples numstudents, grossPay, etc). User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be of spaces or tabs that you use to indent. Use white space to make your program more readable. Part 2: CThere is no part in this assignment Programming 20 points Design a Candy Machine class that can keep track of the type of candies and their quantities. Use the following UML class diagram to design the class CandyMachine. Candy Machine -SELECTION NUM final int- 6 -candy Names stringo -quantities int0 +CandyMachine( +getCandy Name(index int): String +setCandyName(index int, newCandy String): void +get Quantity(index int): int tsetQuantity(index int, numofCandies int) void restock index int, numOfCandies int): void tdecrementQuantity(index int) boolean +total Quantity():int +display Candies(): void +searchindexofCandyName(candyName: String) int Program requirements and/or constraints: You will need to create a file to represent this class CandyMachine,java. The size of the arrays to be used will be 6, and you can use a constant SELECTION NUM to express this. In your class, you declare it as: public class Candy Machine private final int SELECTION NUMExplanation / Answer
public class CandyMachine
{
private final int SELECTION_NUM=6;
String candyNames[];
int quantities[];
CandyMachine()
{
candyNames=new String[SELECTION_NUM];
quantities=new int[SELECTION_NUM];
candyNames[0]="Snickers";
candyNames[1]="Twix";
candyNames[2]="MilkyWay";
candyNames[3]="Almond Joy";
candyNames[4]="Skittles";
candyNames[5]="M&M's";
quantities[0]=5;
quantities[0]=5;
quantities[0]=5;
quantities[0]=5;
quantities[0]=5;
quantities[0]=5;
}
String getCandyName(int index)
{
if(index>=0&&index<=SELECTION_NUM)
{
return candyNames[index];
}
return null;
}
void setCandyName(int index,String newCandy)
{
if(index>=0&&index<=SELECTION_NUM)
{
candyNames[index]=newCandy;
}
}
int getQuantity(int index)
{
if(index>=0&&index<=SELECTION_NUM)
{
return quantities[index];
}
return -1;
}
void setQuantity(int index,int numOfCandies)
{
if(index>=0&&index<=SELECTION_NUM)
{
quantities[index]=numOfCandies;
}
}
boolean decrementQuantity(int index)
{
if(index>=0&&index<=SELECTION_NUM)
{
if(quantities[index]>=1)
{
quantities[index]--;
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
int totalQuantity()
{
int sum=0;
for(int i=0;i<quantities.length;i++)
{
sum=sum+quantities[i];
}
return sum;
}
void displayCandies()
{
System.out.println(String.format("%5s %20s %10s","Candy","Type","Qunatity"));
for(int i=0;i<SELECTION_NUM;i++)
{
System.out.println(String.format("%5d %20s %10d",i,candyNames[i],quantities[i]));
}
System.out.println("");
}
int searchIndexOfCandyName(String candyName)
{
for(int i=0;i<SELECTION_NUM;i++)
{
if(candyNames[i].equals(candyName))
{
return i;
}
}
return -1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.