Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Assignment :Exercise 8

Lab Exercise: CST8116


Due date:6TH of August
Student name: Praneshajay
Student id: 90541103045
Professor: Sapinderjit Kaur

VERSION 1
Understand the Problem
Peewee eggs: 15 oz.

Small eggs: 18 oz.

Medium eggs: 21 oz.

Large eggs: 24 oz.

Extra-large eggs: 27 oz.

Jumbo eggs: 30 oz.

In the program, I’ll need to create an array with 8 elements, one for each of the egg sizes, and two extra for too small
and too large, then use a loop with a sentinel value to ask for egg weights to be input until told to stop. An if statement
can be used to size the eggs: If weight < 15 then too small, if weight < 18 then Peewee, if weight < 21 then Small, if
weight < 24 then medium, if weight < 27 then Large, if weight <30 then extra-large, if weight < 33 then Jumbo, otherwise
too-large.

Algorithm
UML Class Diagrams
Pseudocode
declarations

public class Eggversion1 {

private static final int SIZE = 8;

private int[] eggCounts = new int[SIZE];

private String[] eggSizeNames = {"too small", "peewee", "small", "medium",

"large", "extra-large", "jumbo", "too large"};

public void enterEggs() {

Scanner console = new Scanner(System.in);

String shouldContinue;

double weight;

int size;

do {

System.out.print("Enter egg weight: ");

weight = console.nextDouble();

console.nextLine();

size = sizeEgg(weight);

eggCounts[size] = eggCounts[size] + 1;

System.out.print("Enter another egg weight? (Y/N) ");

shouldContinue = console.nextLine();

} while (shouldContinue.equalsIgnoreCase("Y"));

public void printReport() {

int index = 0;

while(index < SIZE) {

System.out.printf("%s: %d%n", eggSizeNames[index], eggCounts[index]);

index++;

}}

private int sizeEgg(double weight) {

int size;

if (weight < 15) {


size = 0;

else if (weight < 18) {

size = 1;

else if (weight < 21) {

size = 2;

else if (weight < 24) {

size = 3;

else if (weight < 27) {

size = 4;

else if (weight < 30) {

size = 5;

else if (weight < 33) {

size = 6;

else {

size = 7;

return size;

public class

public static void main(String[] args) {

System.out.println("Exercise 08 Version 1");

Eggversion1 counter = new Eggversion1();

counter.enterEggs();

counter.printReport();

System.out.println("Program by Praneshajay");

Flowchart
Test Plan
Input Values Expected Output Actual Output Description
14 too small: 1 too small: 1 Testing boundary for each egg
size & Program works correctly.
Y peewee: 1 peewee: 1
15 small: 1 small: 1
Y medium: 1 medium: 1
18 large: 1 large: 1
Y extra-large: 1 extra-large: 1
21 jumbo: 1 jumbo: 1
Y too large: 1 too large: 1
24
Y
27
Y
30
Y
33
N

Screenshot of Version 1
VERSION 2
Understand the Problem
Peewee eggs: 15 oz.

Small eggs: 18 oz.

Medium eggs: 21 oz.

Large eggs: 24 oz.

Extra-large eggs: 27 oz.

Jumbo eggs: 30 oz.

 In the program, The loop that creates the report was created as a while-loop but I need to convert the logic to use a
for-loop instead.

Pseudocode
public class Eggversion2 {

private static final int SIZE = 8;

private int[] eggCounts = new int[SIZE];

private String[] eggSizeNames = {"too small", "peewee", "small", "medium",

"large", "extra-large", "jumbo", "too large"};

private static final int TOO_SMALL = 0;

private static final int PEEWEE = 1;

private static final int SMALL = 2;

private static final int MEDIUM = 3;

private static final int LARGE = 4;

private static final int EXTRA_LARGE = 5;

private static final int JUMBO = 6;

private static final int TOO_LARGE = 7;

public void enterEggs() {

Scanner console = new Scanner(System.in);

String shouldContinue;

double weight;

int size;

do {

System.out.print("Enter egg weight: ");

weight = console.nextDouble();
console.nextLine();

size = sizeEgg(weight);

eggCounts[size] = eggCounts[size] + 1;

System.out.print("Enter another egg weight? (Y/N) ");

shouldContinue = console.nextLine();

} while (shouldContinue.equalsIgnoreCase("Y"));

public void printReport() {

for(int index = 0; index < SIZE; index++) {

System.out.printf("%s: %d%n", eggSizeNames[index], eggCounts[index]);

private int sizeEgg(double weight) {

int size;

if (weight < 15) {

size = TOO_SMALL;

else if (weight < 18) {

size = PEEWEE;

else if (weight < 21) {

size = SMALL;

else if (weight < 24) {

size = MEDIUM;

else if (weight < 27) {

size = LARGE;

else if (weight < 30) {

size = EXTRA_LARGE;

}
else if (weight < 33) {

size = JUMBO;

else {

size = TOO_LARGE;

return size;

public class Eggversion2main {

public static void main(String[] args) {

System.out.println("Exercise 08 Version 2");

Eggversion2 counter = new Eggversion2();

counter.enterEggs();

counter.printReport();

System.out.println("Program by Praneshajay");

Test Plan
Input Values Expected Output Actual Output Description
14 too small: 1 too small: 1 Testing boundary for each egg
size and program without any
Y peewee: 1 peewee: 1 bugs or errors.
15 small: 1 small: 1
Y medium: 1 medium: 1
18 large: 1 large: 1
Y extra-large: 1 extra-large: 1
21 jumbo: 1 jumbo: 1
Y too large: 1 too large: 1
24
Y
27
Y
30
Y
33
N

Screenshot of Version 2
VERSION 3
Understand the Problem:
The printReport() method already prints out labels, so I can modify it to print the asterisks as well.

A nested loop, that prints one * per iteration based on egg count will work, just before printing the labels.

Before modifying the program, I updated the pseudocode.

Algorithm
Pseudocode
Declarations

public class Eggversion3 {

private static final int SIZE = 8;

private int[] eggCounts = new int[SIZE];

private String[] eggSizeNames = {"too small", "peewee", "small", "medium",

"large", "extra-large", "jumbo", "too large"};

private static final int TOO_SMALL = 0;

private static final int PEEWEE = 1;

private static final int SMALL = 2;


private static final int MEDIUM = 3;

private static final int LARGE = 4;

private static final int EXTRA_LARGE = 5;

private static final int JUMBO = 6;

private static final int TOO_LARGE = 7;

public void enterEggs() {

Scanner console = new Scanner(System.in);

String shouldContinue;

double weight;

int size;

do {

System.out.print("Enter egg weight: ");

weight = console.nextDouble();

console.nextLine();

size = sizeEgg(weight);

eggCounts[size] = eggCounts[size] + 1;

System.out.print("Enter another egg weight? (Y/N) ");

shouldContinue = console.nextLine();

} while (shouldContinue.equalsIgnoreCase("Y"));

public void printReport() {

for(int index = 0; index < SIZE; index++) {

for(int count = 1; count <= eggCounts[index]; count++) {

System.out.print("*");

System.out.printf(" (%s: %d)%n", eggSizeNames[index], eggCounts[index]);

}}

private int sizeEgg(double weight) {

int size;

if (weight < 15) {

size = TOO_SMALL;
}

else if (weight < 18) {

size = PEEWEE;

else if (weight < 21) {

size = SMALL;

else if (weight < 24) {

size = MEDIUM;

else if (weight < 27) {

size = LARGE;

else if (weight < 30) {

size = EXTRA_LARGE;

else if (weight < 33) {

size = JUMBO;

else {

size = TOO_LARGE;

return size;

public class Eggversion3main {

public static void main(String[] args) {

System.out.println("Exercise 08 Version 3");

Eggversion3 counter = new Eggversion3();

counter.enterEggs();

counter.printReport();

System.out.println("Program by Praneshajay");

Test Plan
Input Values Expected Output Actual Output Description
14 Testing bar chart outputs.
Y
14 The number of * correspond to
Y the number of eggs counted for
14 each size.
Y *** (too small: 3) *** (too small: 3)
15
Y
15
Y ** (peewee: 2) ** (peewee: 2)
21 (small: 0 ,no 18,19,20 (small: 0 ,no 18,19,20
Y have entered) have entered)
24 * (medium: 1) * (medium: 1)
Y
27 * (large: 1) * (large: 1)
Y
30 * (extra-large: 1) * (extra-large: 1)
Y
30
Y
30
Y
30
Y
33 **** (jumbo: 4) **** (jumbo: 4)
n
* (too large: 1) * (too large: 1)
Screenshot of Version 3

You might also like