Java Programming And Object Oriented Programming English Language Essay

Published:

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

The beginning phrase, import java.io means that the certain class is been called so that its methods could be inherited and used simultaneously throughout the program which the user is writing.

Basically, we are trying to import the methods from the class, 'java.io' to be inherited in the program.

If that phrase is omitted, the program would run flawlessly because the class declared isn't needed or used for any methods in this following program.

c. The sole purpose of the asterisk, '*' is to call all the sub-classes and methods in the main class 'java.io'.

d. Basically, a class is a library in Java which holds many methods which could be inherited to be used in other user's programs.

The sole distinction between a class and an object would be that a class is a real structure containing all the methods within itself. But an object is an implication of a specific class which is being declared in that specific program's context which in return references to the class.

e. The keyword 'public' simply means that the declared method main, is accessible by any other classes created in that specific program's package or any other outside programs and packages.

f. The keyword 'static' simply indicates that the contents of that variable or methods would remain the same throughout the program's context.

g. The keyword 'void' indicates that the method main doesn't return any arguments.

h. The keyword 'main' is the name of the method which also indicates the beginning of the program.

i. The semi-colon after the "Hello, World!" indicates the end of the current statement allowing the compiler to proceed to the next sentence.

j. One of the methods found in this program is the 'main' method.

2. Data Types

Whole (counting) numbers:

Name

Bytes of Storage taken up

Range

Example

2.1

int

4

-2,147,483,648 to 2,147,483,647

42

2.2

short

2

-32,768 to -32,767

17

2.3

long

8

-922,337,203,685,477,5808 to 922,337,203,685,477,5807

34644322

2.4

byte

1

-128 to 127

100

Fractions (decimal numbers):

Name

Bytes of Storage taken up

Number of significant digits

Example

2.5

float

4

7

3.141592

2.6

double

8

15

3.14159265358979

Single Characters:

Name

Bytes of Storage taken up

Example

2.4

char

2

c

'Logical outcomes': true or false

Name

Bytes of Storage taken up

Example

2.4

boolean

1

true

3. Variables

3.1. Variables are like containers which are meant to store the data in a program.

3.2. Well, writing the data-type before writing the variable's name in a statement, assigns that variable to hold only that data-type's range of values which are permissible to be contained in that data-type.

3.3. An error might occur if the range of the data-type assigned exceeds the variable's contents.

3.4.a. foreign: This word is permissible as a variable.

3.4.b. native: This word is not permissible as a variable because it's a reserved keyword.

3.4.c. purchaselimit: This word is permissible as a variable.

3.4.d. purchase limit: This word is not permissible as a variable because a variable with a blank space in between them would not be accepted as a variable by the compiler.

4. Operators

4.1.

Arithmetic Signs

Arithmetic Definitions

Example

Result

+

Addition

5 + 7

12

-

Subtraction

9 - 2

7

*

Multiplication

5 * 5

25

/

Division

25 / 5

5

%

Modulus / Remainder

23 % 10

3

4.2.

Assignment:

d = i * 3 / 7; // 'd' is assigned the value of 'i' been multiplied with the result of '3/9'.

4.3.

Relational operator signs

Operator Definitions

Example

Result

<

Less than

0 < 1

true

>

Greater than

0 > 1

false

<=

Less than or equals to

0 <= 1

true

>=

Greater than or equals to

0 >= 1

false

!=

Not equals to

0 != 1

true

==

Equals to

0 == 1

false

The results would be printed in boolean form as both the integers '0' and '1' are been compared against the relational operators.

4.4.

Truth table for '&&' operators

X

Y

X && Y

true

true

true

true

false

false

false

true

false

false

false

false

- '&&' operator is an AND operator and it does the operations similar to the AND gate.

Truth table for '||' operators

X

Y

X && Y

true

true

true

true

false

true

false

true

true

false

false

false

- '||' operator is an OR operator and it does the operations similar to the OR gate.

5. Control Structures

5.1. Making Choices:

5.1.1. An example of an if-else statement:

bool MOUSE_CLICKED = false;

if (MOUSE_CLICKED)

{

// Carry out the statements found in this if-block

}

else

{

// If MOUSE_CLICKED doesn't points to 'TRUE' value then, this else-block executes.

}

5.1.2. An example of a switch statement:

Scanner input = new Scanner(new System.in);

System.out.println("Enter any binary digit 0 or 1 : ");

byte numberUserEnters =(byte) input.nextInt();

switch(numberUserEnters)

{

case 0 : System.out.println("LOGIC LOW!"); break;

case 1 : System.out.println("LOGIC HIGH!"); break;

}

5.2. Doing something repeatedly:

5.2.1. An example of a while statement:

Scanner input = new Scanner(new System.in);

int[] STORE = new int[5];

int counter = 0;

while ( counter >= 5 );

{

System.out.print("Enter any integer : ");

STORE[counter] = input.nextInt();

System.out.println();

counter++;

}

5.2.2. An example of a do-while statement:

Scanner input = new Scanner(System.in);

int[] STORE = new int[5];

int counter = 0;

do

{

System.out.print("Enter some integers : ");

STORE[counter] = input.nextInt();

System.out.println();

counter++;

}while(counter <= 4);

5.2.3. An example of a for statement:

Scanner input = new Scanner(System.in);

int[] STORE = new int[5];

for (int counter = 0; counter <= 4; counter++)

{

System.out.print("Enter some numbers : ");

STORE[counter] = input.nextInt();

System.out.println();

}

B. Programming Practice

1. Simple Comparison I

Program Code:

import java.util.*;

public class CompareInt

{

// Global Variables

private static int[] UNSORTED = new int[3];

private static int[] SORTED = new int[3];

// -------------------- CONSTRUCTORS ------------------------ //

// A Constructor holding the algorithm of the sorting method

CompareInt(int[] passedNUMBERS)

{

// The local variables is passed to the global variables

for(int index = 0; index < 3; index++)

{

UNSORTED[index] = passedNUMBERS[index];

}

// Algorithm process to sort out the integers in the array

// THE COMPARISON OF THE INTEGERS ONE VERSUS THE OTHER TWO

// if the First integer is smaller than the other two integers in // the array

if(UNSORTED[0] < UNSORTED[1] && UNSORTED[0] < UNSORTED[2])

{

// The first integer from the unsorted array is assigned // to the first space of the sorted array

SORTED[0] = UNSORTED[0];

// And the second integer of the unsorted array is

// compared against the third one

if (UNSORTED[1] < UNSORTED[2])

{

// Provided the second integer is smaller than // the third integer, the second number from the // unsorted array is assigned to the second // space of the sorted array whilst the third // integer from the unsorted array is assigned // to the third space of the sorted array.

SORTED[1] = UNSORTED[1];

SORTED[2] = UNSORTED[2];

}

else

{

// Else, provided the second integer is greater than the third integer, the third number from the unsorted array is assigned to the second space of the sorted array whilst the second integer from the unsorted array is assigned to the third space of the sorted array.

SORTED[1] = UNSORTED[2];

SORTED[2] = UNSORTED[1];

}

}

// else-if the Second integer is smaller than the other two // integers in the array

else if(UNSORTED[1] < UNSORTED[0] && UNSORTED[1] < UNSORTED[2])

{

// The second integer from the unsorted array is assigned // to the first space of the sorted array

SORTED[0] = UNSORTED[1];

// And the first integer of the unsorted array is compared // against the third one

if(UNSORTED[0] < UNSORTED[2])

{

// Provided the first integer is smaller than // the third integer, the first number from the // unsorted array is assigned to the second // space of the sorted array whilst the third // integer from the unsorted array is assigned // to the third space of the sorted array.

SORTED[1] = UNSORTED[0];

SORTED[2] = UNSORTED[2];

}

else

{

// Else, provided the first integer is greater // than the third integer, the third number from // the unsorted array is assigned to the second // space of the sorted array whilst the first // integer from the unsorted array is assigned // to the third space of the sorted array.

SORTED[1] = UNSORTED[2];

SORTED[2] = UNSORTED[0];

}

}

// else-if the Third integer is smaller than the other two // integers in the array

else if(UNSORTED[2] < UNSORTED[0] && UNSORTED[2] < UNSORTED[1])

{

// The second integer from the unsorted array is assigned // to the first space of the sorted array

SORTED[0] = UNSORTED[2];

// And the first integer of the unsorted array is compared // against the second one

if(UNSORTED[0] < UNSORTED[1])

{

// Provided the first integer is smaller than // the second integer, the first number from the unsorted array is assigned to the second space of the sorted array whilst the second integer from the unsorted array is assigned to the third space of the sorted array.

SORTED[1] = UNSORTED[0];

SORTED[2] = UNSORTED[1];

}

else

{

// Else, provided the first integer is greater // than the second integer, the second number // from the unsorted array is assigned to the // second space of the sorted array whilst the // first integer from the unsorted array is // assigned to the third space of the sorted // array.

SORTED[1] = UNSORTED[1];

SORTED[2] = UNSORTED[0];

}

}

}

// ------------------- METHODS ------------------------------ //

// Sub-Method

public static int getInteger()

{

Scanner INPUT = new Scanner(System.in);

return INPUT.nextInt();

}

// Main Method

public static void main(String[] args)

{

// Local Variables

String[] WholeNumberPrompts = {"First number : ", "Second number : ", "Third number : "};

int[] NUMBERS = new int[3];

System.out.println("Enter three whole numbers.");

// Get the integers from the User and Store in a Array

for(int counter = 0; counter < 3; counter++)

{

System.out.print(WholeNumberPrompts[counter]);

NUMBERS[counter] = getInteger();

}

// Pass the numbers to the class method

CompareInt objectCompareInt = new CompareInt(NUMBERS);

System.out.println("\nThe smallest number is : " + CompareInt.SORTED[0]);

System.out.println("The middle number is : " + CompareInt.SORTED[1]);

System.out.println("The largest number is : " + CompareInt.SORTED[2]);

}

}

Screenshot of a Typical Run:

Problems Encountered:

I had problems keeping track of the if-else loops which were repeating itself and the braces which were omitted accidently was causing a headache. And programming a "Little-Endian" was tedious as the numbers needs to be compared against the same pair for two times and it was repeating itself again and again.

Debugging Methods Used:

As for the braces which were left out, there weren't other choices left for me, so I manually went through the if-else loops to figure out the braces properly assigned to the individual loops.

2. Simple Comparison II

Program Code:

import java.util.Scanner;

public class CompareChar

{

// Global Variables

private static char[] UNSORTED = new char[3];

private static char[] SORTED = new char[3];

// -------------------- CONSTRUCTORS ------------------------ //

// A Constructor holding the algorithm of the sorting method

CompareChar(char[] passedCharacters)

{

UNSORTED = passedCharacters;

// THE COMPARISON OF THE ALPHABETS ONE VERSUS THE OTHER TWO

// if the First alphabet is smaller than the other two alphabets in // the array

if(UNSORTED[0] < UNSORTED[1] && UNSORTED[0] < UNSORTED[2])

{

// The first alphabet from the unsorted array is assigned to the // first space of the sorted array

SORTED[0] = UNSORTED[0];

// And the second alphabet of the unsorted array is compared // against the third one

if (UNSORTED[1] < UNSORTED[2])

{

// Provided the second alphabet is smaller than the // third alphabet, the second alphabet from the // unsorted array is assigned to the second space of // the sorted array whilst the third alphabet from the // unsorted array is assigned to the third space of the

// sorted array.

SORTED[1] = UNSORTED[1];

SORTED[2] = UNSORTED[2];

}

else

{

// Else, provided the second alphabet is greater than // the third alphabet, the third alphabet from the // unsorted array is assigned to the second space of // the sorted array whilst the second alphabet from the // unsorted array is assigned to the third space of the

// sorted array.

SORTED[1] = UNSORTED[2];

SORTED[2] = UNSORTED[1];

}

}

// else-if the Second alphabet is smaller than the other two alphabets // in the array

else if(UNSORTED[1] < UNSORTED[0] && UNSORTED[1] < UNSORTED[2])

{

// The second alphabet from the unsorted array is assigned to the // first space of the sorted array

SORTED[0] = UNSORTED[1];

// And the first alphabet of the unsorted array is compared // against the third one

if(UNSORTED[0] < UNSORTED[2])

{

// Provided the first alphabet is smaller than the // third alphabet, the first alphabet from the unsorted // array is assigned to the second space of the sorted // array whilst the third alphabet from the unsorted // array is assigned to the third space of the

// sorted array.

SORTED[1] = UNSORTED[0];

SORTED[2] = UNSORTED[2];

}

else

{

// Else, provided the first alphabet is greater than // the third alphabet, the third alphabet from the // unsorted array is assigned to the second space of // the sorted array whilst the first alphabet from the // unsorted array is assigned to the third space of the

// sorted array.

SORTED[1] = UNSORTED[2];

SORTED[2] = UNSORTED[0];

}

}

// else-if the Third alphabet is smaller than the other two alphabets in // the array

else if(UNSORTED[2] < UNSORTED[0] && UNSORTED[2] < UNSORTED[1])

{

// The second alphabet from the unsorted array is assigned to the // first space of the sorted array

SORTED[0] = UNSORTED[2];

// And the first alphabet of the unsorted array is compared // against the second one

if(UNSORTED[0] < UNSORTED[1])

{

// Provided the first alphabet is smaller than the // second alphabet, the first alphabet from the // unsorted array is assigned to the second space of // the sorted array whilst the second alphabet from the // unsorted array is assigned to the third space of the

// sorted array.

SORTED[1] = UNSORTED[0];

SORTED[2] = UNSORTED[1];

}

else

{

// Else, provided the first alphabet is greater than // the second alphabet, the second alphabet from the // unsorted array is assigned to the second space of // the sorted array whilst the first alphabet from the // unsorted array is assigned to the third space of the

// sorted array.

SORTED[1] = UNSORTED[1];

SORTED[2] = UNSORTED[0];

}

}

}

// ------------------- METHODS ------------------------------ //

// Sub-Method

public static char getCharacter()

{

Scanner INPUT = new Scanner(System.in);

return INPUT.nextLine().charAt(0);

}

// Main Method

public static void main(String[] args)

{

// Local Variables

String[] WholeNumberPrompts = {"First alphabet : ", "Second alphabet :", "Third alphabet : "};

char[] ALPAHBETS = new char[3];

System.out.println("Enter three distinct alphabetic characters.");

for(int counter = 0; counter < 3; counter++)

{

System.out.print(WholeNumberPrompts[counter]);

ALPAHBETS[counter] = getCharacter();

}

CompareChar objectCompareChar = new CompareChar(ALPAHBETS);

System.out.println("\nThe smallest alphabet is : " + CompareChar.SORTED[0]);

System.out.println("The middle alphabet is : " + CompareChar.SORTED[1]);

System.out.println("The largest alphabet is : " +CompareChar.SORTED[2]);

}

}

Screenshot of a Typical Run:

Problems Encountered:

This question put me in a confusion state but soon I was convinced by the nature of the question and decided to use ASCII/Unicode to sort the alphabets as the question didn't mention anything about the nature of the arrangement of the alphabets.

Therefore I didn't had to do much alteration to the previous program but fortunately I discovered that this program could solve the next question which prompts for the symbols and print it out in an ascending order too.

Debugging Methods Used:

As there weren't any problems encountered except for an extra function being used here to read in extra characters other than alphabets, there was not a need to debug anything.

3. Simple Comparison III

Program Code:

import java.util.Scanner;

public class CompareRandomChar

{

// Global Variables

private static char[] UNSORTED = new char[3];

private static char[] SORTED = new char[3];

// -------------------- CONSTRUCTORS ------------------------ //

// A Constructor holding the algorithm of the sorting method

CompareRandomChar(char[] passedCharacters)

{

UNSORTED = passedCharacters;

// THE COMPARISON OF THE ALPHABETS ONE VERSUS THE OTHER TWO //

// if the First alphabet is smaller than the other two alphabets in // the array

if(UNSORTED[0] < UNSORTED[1] && UNSORTED[0] < UNSORTED[2])

{

// The first alphabet from the unsorted array is assigned to the // first space of the sorted array

SORTED[0] = UNSORTED[0];

// And the second alphabet of the unsorted array is compared // against the third one

if (UNSORTED[1] < UNSORTED[2])

{

// Provided the second alphabet is smaller than the // third alphabet, the second alphabet from the // unsorted array is assigned to the second space of // the sorted array whilst the third alphabet from the // unsorted array is assigned to the third space of the

// sorted array.

SORTED[1] = UNSORTED[1];

SORTED[2] = UNSORTED[2];

}

else

{

// Else, provided the second alphabet is greater than // the third alphabet, the third alphabet from the // unsorted array is assigned to the second space of // the sorted array whilst the second alphabet from the // unsorted array is assigned to the third space of the

// sorted array.

SORTED[1] = UNSORTED[2];

SORTED[2] = UNSORTED[1];

}

}

// else-if the Second alphabet is smaller than the other two alphabets //in the array

else if(UNSORTED[1] < UNSORTED[0] && UNSORTED[1] < UNSORTED[2])

{

// The second alphabet from the unsorted array is assigned to the // first space of the sorted array

SORTED[0] = UNSORTED[1];

// And the first alphabet of the unsorted array is compared // against the third one

if(UNSORTED[0] < UNSORTED[2])

{

// Provided the first alphabet is smaller than the // third alphabet, the first alphabet from the unsorted // array is assigned to the second space of the sorted // array whilst the third alphabet from the unsorted // array is assigned to the third space of the

// sorted array.

SORTED[1] = UNSORTED[0];

SORTED[2] = UNSORTED[2];

}

else

{

// Else, provided the first alphabet is greater than // the third alphabet, the third alphabet from the // unsorted array is assigned to the second space of // the sorted array whilst the first alphabet from the // unsorted array is assigned to the third space of the

// sorted array.

SORTED[1] = UNSORTED[2];

SORTED[2] = UNSORTED[0];

}

}

// else-if the Third alphabet is smaller than the other two alphabets in // the array

else if(UNSORTED[2] < UNSORTED[0] && UNSORTED[2] < UNSORTED[1])

{

// The second alphabet from the unsorted array is assigned to the // first space of the sorted array

SORTED[0] = UNSORTED[2];

// And the first alphabet of the unsorted array is compared // against the second one

if(UNSORTED[0] < UNSORTED[1])

{

// Provided the first alphabet is smaller than the // second alphabet, the first alphabet from the // unsorted array is assigned to the second space of // the sorted array whilst the second alphabet from the // unsorted array is assigned to the third space of the

// sorted array.

SORTED[1] = UNSORTED[0];

SORTED[2] = UNSORTED[1];

}

else

{

// Else, provided the first alphabet is greater than // the second alphabet, the second alphabet from the // unsorted array is assigned to the second space of // the sorted array whilst the first alphabet from the // unsorted array is assigned to the third space of the

// sorted array.

SORTED[1] = UNSORTED[1];

SORTED[2] = UNSORTED[0];

}

}

}

// ------------------- METHODS ------------------------------ //

// Sub-Method

public static char getCharacter()

{

Scanner INPUT = new Scanner(System.in);

return INPUT.nextLine().charAt(0);

}

// Main Method

public static void main(String[] args)

{

// Local Variables

String[] WholeNumberPrompts = {"First character :", "Second character:", "Third character : "};

char[] CHARACTERS = new char[3];

System.out.println("Enter any three distinct characters.");

for(int counter = 0; counter < 3; counter++)

{

System.out.print(WholeNumberPrompts[counter]);

CHARACTERS[counter] = getCharacter();

}

CompareRandomChar objectCompareChar = new CompareRandomChar(CHARACTERS);

System.out.println("\nThe smallest character is : " + CompareRandomChar.SORTED[0]);

System.out.println("The middle character is : " + CompareRandomChar.SORTED[1]);

System.out.println("The largest character is : " + CompareRandomChar.SORTED[2]);

}

}

Screenshot of a Typical Run:

Problems Encountered:

This question wasn't any different from the previous comparison question. So no problems occurred with this program for me.

Debugging Methods Used:

No debugging was used on this program.

4. Learning about formatting your output

Program Code:

import java.text.*;

public class B04

{

// This method would print Empty Line Breaks for the number of arguments // been passed

public static void LINEBREAK(int noOfLineBreaks)

{

for(int counter = 0; counter < noOfLineBreaks; counter++)

{

System.out.println();

}

}

public static void main(String[] args)

{

// This segment illustrates numbers to be printed with different // precisions and widths

// An example would be utilised to illustrate that below:

double DECIMAL_NUMBER = 8.0 / 3;

System.out.println("The double value by default is " + DECIMAL_NUMBER +

"\n");

// The decimal printed to three decimal places and a width of 5

System.out.printf("The decimal value printed to the nearest three"+

"places and a width of 5 would be %5.3f", DECIMAL_NUMBER);

// Print a Line Break

LINEBREAK(1);

// The decimal printed to one decimal places and a width of 3

System.out.printf("The decimal value printed to the nearest ONE place" + "and a width of 3 would be %3.1f", DECIMAL_NUMBER);

LINEBREAK(3);

// This segment illustrates rows of String be printed with different // justifications

String[] NAMES = {"Sam", "Tom", "Harry", "Michael", "Steve"};

// Several rows of names being printed with left justification

System.out.println("Left Justified : ");

for (int counter = 0; counter < 5; counter++)

{

System.out.printf("%-8s", NAMES[counter]);

}

LINEBREAK(2);

// Several rows of names being printed with right justification

System.out.println("Right Justified : ");

for (int counter = 0; counter < 5; counter++)

{

System.out.printf("%8s", NAMES[counter]);

}

LINEBREAK(3);

// Line up several decimal numbers aligned accordingly to their

double[] values = {1.41, 12.44, 767.98, 2341.9, 1.98346};

// Several rows of decimal numbers printed aligned to their

// decimal points

System.out.println("Decimals Alligned accordingly to their decimal" + "points: \n\n");

for (int counter = 0; counter < 5; counter++)

{

// The Decimal's would hold a width of 21 and a mantissa of // 15 would be supported as illustrated by the following // statement

System.out.printf("%21.15f", values[counter]);

LINEBREAK(1);

}

}

}

Screenshot of a Typical Run:

Problems Encountered:

I had no problems with this program's formatting functions except for the last alignment part of the double values which are aligned according to their decimal dots before the mantissa.

Debugging Methods Used:

As there were some issues with the alignment part of the double values' dots, I had to manually edit the program's prinf method and explore on that class's functionality to produce such a output.

5. The Guessing Game

Program Code:

import java.util.*;

public class B05

{

public static void GuessingGame()

{

// START OF DECLARATION OF VARIABLES & CLASSES //

// The boolean would be used to control the do-while loop later

boolean Solved = false;

// This variables would be used to keep the random numbers and the // user's numbers created in two temp variables

int randomUser, randomCPU;

// Scanner class created to read in console input

Scanner input = new Scanner(System.in);

// Random class created

Random random = new Random();

// Random numbers which are created until 1000 are assigned to the // temp variable

randomCPU = random.nextInt(1000);

// ----- END OF DECLARATION OF VARIABLES ------------ //

System.out.println("Guess my number! It's between 1 and 1000");

// This loop would be executed at least one time and depending on // the boolean variable, 'Solved'

do

{

System.out.print("Your guess :");

// Asks for the user's guessed integer

randomUser = input.nextInt();

// If the number entered by the user is bigger than number // generated by the CPU, make the boolean true to go through // the loop for another time

if (randomUser < randomCPU)

{

System.out.println("Too low!");

Solved = true;

}

// If the number entered by the user is smaller than number // generated by the CPU, make the boolean true to go through // the loop for another time

else if (randomUser > randomCPU)

{

System.out.println("Too high!");

Solved = true;

}

// If the number entered by the user is equals to the number // generated by the CPU, make the boolean false to end the // loop

else if (randomUser == randomCPU)

{

System.out.println("You got it!");

Solved = false;

}

}while(Solved);

}

}

Screenshot of a Typical Run:

Problems Encountered:

This was the easiest program I came across in this assignment and I had no issues with it at all.

Debugging Methods Used:

As there wasn't any problems encountered I didn't had the hassle to debug it.

6. The Shopping Cart I

Program Code:

import java.util.*;

public class B06

{

// This method keeps track of the Quantity of the individual Products been // sold and returns back into the array

public static int getQuantity()

{

Scanner input = new Scanner(System.in);

System.out.print("Enter Quantity : ");

return input.nextInt();

}

public static void ShoppingCart1()

{

// START OF DECLARATION OF VARIABLES & CLASSES

Scanner input = new Scanner(System.in);

String UserInputToExitLoop = "";

int HowManyItemsBought = 0;

double TotalAmount = 0;

// This array stores the Product Codes of the Store Items

String[] StoreItems = {"0", "P3487", "K3876", "Z9983"};

// This array stores the Prices of the Store Items

double[] StorePrices = {0, 2.50, 3.00, 12.75};

// This column of arrays' stack keep track of the Products' // information being purchased

String[] BuyingItems = new String[4];

String[] BuyingItemsName = new String[4];

double[] BuyingPrices = new double[4];

int[] BuyingQuantity = new int[4];

// ---------- END OF DECLARATION OF VARIABLES ------------ //

System.out.println("Our catalog [Product Codes in brackets]:");

System.out.println("(1) Condensed Powdered water [P3487], $2.50 per"+ "packet.");

System.out.println("(2) Distilled Moonbeams [K3876], $3.00 a dozen.");

System.out.println("(3) Anti-Gravity Pills [Z9983], $12.75 for 60.");

System.out.println("Buy something!");

// The maximum items that could be purchased is 4, therefore I am // limiting the loop to 4 considering the fact that a user could // only purchase 4 items as the maximum

for (int counter = 0; counter < 4; counter++)

{

System.out.print("Enter Order Number (0 to stop) : ");

// The User Input which is the PRODUCT CODE in stored in a // String Variable

UserInputToExitLoop = input.nextLine();

// The String Variable is compared against a set conditions // which are the SET OF PRODUCT CODES

// Upon matching the PRODUCT CODE, a set of instructions // would be executed. They are as follows,

// The Item's Product Codes, Product Prices, Product's Names // and their Quantity are stored in four different arrays // with respect to their address values

switch (UserInputToExitLoop)

{

case "P3487": BuyingItems[counter] = "P3487"; BuyingPrices[counter] = 2.5; BuyingItemsName[counter] = "Condensed"+ "Powdered water";

BuyingQuantity[counter] = getQuantity(); break;

case "K3876": BuyingItems[counter] = "K3876"; BuyingPrices[counter] = 3; BuyingItemsName[counter] = "Distilled"+ "Moonbeams";

BuyingQuantity[counter] = getQuantity(); break;

case "Z9983": BuyingItems[counter] = "Z9983"; BuyingPrices[counter] = 12.75; BuyingItemsName[counter] = "Anti-Gravity"+ "Pills";

BuyingQuantity[counter] = getQuantity(); break;

// If '0' is entered, the program breaks out of the // loop by initialising counter to 4

case "0": HowManyItemsBought = counter;

counter = 4; break;

// If any other User Input is entered, other than the // above ones, then the counter would decrement itself

// and prompt for the Order Number as usual

default: counter--;

System.out.println("Sorry, I don't understand! Use"+ "Product Codes only.");

}

}

// This segment prints out the Order Summary of the Products been // purchased

System.out.print("Your order : ");

// The multiple arrays which store the Purchased Items' Information // are being printed on the screen

// with respect to their address values

for (int counter = 0; counter < HowManyItemsBought; counter++)

{

System.out.println(BuyingQuantity[counter] + " " + BuyingItemsName[counter] + " @ " + BuyingPrices[counter] +

" = " + BuyingPrices[counter]*BuyingQuantity[counter]);

TotalAmount += BuyingPrices[counter] * BuyingQuantity[counter];

}

System.out.print("Subtotal: $" + TotalAmount + " ");

System.out.printf("Tax @ 20%%: $ %7.2f", (0.2*TotalAmount));

System.out.print(" Grand Total: $" + (TotalAmount + (0.2*TotalAmount)));

}

}

Screenshot of a Typical Run:

Problems Encountered:

I hadn't much problems with this program except for coming out with the algorithms was the challenging bit, which I found in this program. But getting each product code seemed like a easy part but the processing which took place behind each prompt of the product code was the crucial part of the program, where each product code purchased needs to store the products' names, its' prices, and the quantity bought in the arrays specified in its specified array address locations with respect to their index number of the arrays.

Debugging Methods Used:

Programmatically, there weren't any problems but there was a issue with the algorithms where there was more than one way of solving the issue of storing the purchased products information; one thru arrays and another through Strings and Searching functions. But as the Strings method was tedious, I resorted to the arrays methods which were less complicated.

7. The Shopping Cart II

Program Code:

import java.util.Scanner;

public class B07

{

// This method keeps track of the Quantity of the individual Products been // sold and returns back into the array

public static int getQuantity()

{

Scanner input = new Scanner(System.in);

int getQuantityInput, returnQuantity = 0;

System.out.println("Enter Quantity (1 to 100): ");

getQuantityInput = input.nextInt();

// if Quantity is more than 1 and less than or equals to 100, then // the program accepts the Quantity Value

if (getQuantityInput > 1 && getQuantityInput <= 100)

{

returnQuantity = getQuantityInput;

}

else

{

System.out.println("The Quantity is out of bounds, please try"+ "again... ");

getQuantity();

}

return returnQuantity;

}

public static void ShoppingCart2()

{

// ------ START OF DECLARATION OF VARIABLES & CLASSES ----------- //

Scanner input = new Scanner(System.in);

String UserInputToExitLoop = "";

int HowManyItemsBought = 0;

double TotalAmount = 0;

// This array stores the Product Codes of the Store Items

String[] StoreItems = {"0", "P3487", "K3876", "Z9983"};

// This array stores the Prices of the Store Items

double[] StorePrices = {0, 2.50, 3.00, 12.75};

// This column of arrays' stack keep track of the Products' // information being purchased

String[] BuyingItems = new String[4];

String[] BuyingItemsName = new String[4];

double[] BuyingPrices = new double[4];

int[] BuyingQuantity = new int[4];

// ---------- END OF DECLARATION OF VARIABLES ------------ //

System.out.println("Our catalog [Product Codes in brackets]:");

System.out.println("(1) Condensed Powdered water [P3487], $2.50 per" +"packet.");

System.out.println("(2) Distilled Moonbeams [K3876], $3.00 a dozen.");

System.out.println("(3) Anti-Gravity Pills [Z9983], $12.75 for 60.");

System.out.println("Buy something!");

// The maximum items that could be purchased is 4, therefore I am // limiting the loop to 4 considering the fact that a user could // only purchase 4 items as the maximum

for (int counter = 0; counter < 4; counter++)

{

System.out.println("Enter Order Number (0 to stop) : ");

// The User Input which is the PRODUCT CODE in stored in a // String Variable

UserInputToExitLoop = input.nextLine();

// This Try Function would help catch any errors for the // program to run effectively even if the User enters

// characters less than '5' characters wide

try

{

// The String's First Character is verified whether // it's a Alphabet

if(Character.isLetter(UserInputToExitLoop.charAt(0)))

{

// The String's Second, Third, and Fourth // characters are verified whether it's a // numeral

if(Character.isDigit(UserInputToExitLoop.charAt(1)) || Character.isDigit(UserInputToExitLoop.charAt(2)) || Character.isDigit(UserInputToExitLoop.charAt(3)) || Character.isDigit(UserInputToExitLoop.charAt(4)))

{

// The String variable is compared against // a set of conditions which are the SET // of PRODUCT CODES

// Upon matching the PRODUCT CODE, a set // of instructions would be executed. They // are as follows,

// The Item's Product COdes, Product // Prices, Product's Names and their // Quantity are stored

// in four different arrays with respect // to their address values

if(UserInputToExitLoop.equals("P3487"))

{

BuyingItems[counter] = "P3487"; BuyingPrices[counter] = 2.5; BuyingItemsName[counter] = "Condensed"+ "Powdered water"; BuyingQuantity[counter] =getQuantity();

}

else if(UserInputToExitLoop.equals("K3876"))

{

BuyingItems[counter] = "K3876"; BuyingPrices[counter] = 3; BuyingItemsName[counter] = "Distilled"+ "Moonbeams";

BuyingQuantity[counter] =getQuantity();

}

else if(UserInputToExitLoop.equals("Z9983"))

{

BuyingItems[counter] = "Z9983"; BuyingPrices[counter] = 12.75; BuyingItemsName[counter] = "Anti"+

"Gravity Pills"; BuyingQuantity[counter] =getQuantity();

}

else

{

System.out.println("I don't recognize"+ "that Product Code");

counter--;

}

}

}

// Else if the String's First Character is a numeral, // it should be either a '0' or any other number, so // it's compared for any other numbers and zero

else if(Character.isDigit(UserInputToExitLoop.charAt(0)))

{

if(UserInputToExitLoop.charAt(0) == '0')

{

HowManyItemsBought = counter; counter = 4;

}

else

{

System.out.println("Sorry, I don't"+ "understand! Use Product Codes only."); counter--;

}

}

}

catch(Exception ex)

{

System.out.println("Error Occured! Please try again with"+ "your input"); counter--;

}

}

// This segment prints out the Order Summary of the Products been // purchased

System.out.println("Your order : ");

// The multiple arrays which store the Purchased Items' Information // are being printed on the screen with respect to their // address values

for (int counter = 0; counter < HowManyItemsBought; counter++)

{

System.out.println(BuyingQuantity[counter] + " " + BuyingItemsName[counter] + " @ " + BuyingPrices[counter] + " = " + BuyingPrices[counter]*BuyingQuantity[counter]);

TotalAmount = BuyingPrices[counter] * BuyingQuantity[counter];

}

System.out.print("Subtotal: $" + TotalAmount + " ");

System.out.printf("Tax @ 20%%: $ %7.2f", (0.2*TotalAmount));

System.out.print(" Grand Total: $" + (TotalAmount +

(0.2*TotalAmount)));

}

}

Screenshot of a Typical Run:

Problems Encountered:

This second part of the program didn't really gave my any additional problems as this was just a mere add-on to the previous program.

Debugging Methods Used:

As there weren't any problems encountered except for a extra function being used here to read in extra characters other than alphabets, there was not a need to debug anything.

8. Loops within Loops I

Program Code:

public class B08

{

public static void Loops1()

{

// The Topmost and the below most are been stored in a array

char[] topNBtmCOLUMN = {'X', 'X', 'X', ' ', ' ', ' ', 'X', 'X', 'X'};

// The Middle Part are been stored in a array too

char[] midCOLUMN = {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'};

// Two counters are being utilised here, one is the vertical counter // to keep track of the Columns and horizontal counter to keep track // of the Rows.

// For every Vertical Loop, a Horizontal Loop would execute printing // the entries from the arrays

// but when the Vertical Counter hits the middle row, which is 3 is // this context, it would execute the 'midCOLUMN' array from the // context

for(int verticalCounter = 0; verticalCounter < 7; verticalCounter++)

{

if(verticalCounter != 3)

{

for(int horizontalCounter = 0; horizontalCounter < 9; horizontalCounter++)

{

System.out.print(topNBtmCOLUMN[horizontalCounter]);

}

}

else

{

for(int horizontalCounter = 0; horizontalCounter < 9; horizontalCounter++)

{

System.out.print(midCOLUMN[horizontalCounter]);

}

}

// This prints a line after every Horizontal Array being printed

System.out.println();

}

}

}

Screenshot of a Typical Run:

Problems Encountered:

No major problems occurred to be addressed.

Debugging Methods Used:

As there weren't any problems encountered, there was not a need to debug anything.

9. Loops within Loops II

Program Code:

import java.util.*;

public class B09

{

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);

int width = 7;

// Create the 'X'

// This prints the Row

for(int row = 1; row <= width; row++)

{

System.out.println();

// This prints the Column

for(int column = 1; column <= width; column++)

{

// The line would be drawn to form the one side of the 'X' exactly when // the Row's and the Columns

// having the same axises.

// Same occurs for the other opposite side by negatively.

if(row == column || (row + column) == (width + 1))

System.out.print("X");

else

System.out.print(" ");

}

}

}

}

Screenshot of a Typical Run:

Problems Encountered:

I had some problems with designing of the 'X' where the odd numbers of the width size was causing a hindrance to the 'X' I created programmatically. For example, when I used an even number of width and that made the middle part crowded with four X's and that wasn't what a 'X' typically could be. So I got to use two conditions, one is to control the number of rows to be equivalent to the number of columns and other was to control the width to be even.

Debugging Methods Used:

I had some issues with the alignment of the 'X's into the correct columns and the problematic part was when I need to create a condition to create the 'X' in the correct position. So a little trial and error was needed in action to test out the codes.

10. Loops within Loops III

Program Code:

import java.util.Scanner;

public class B10

{

public static void Loops3()

{

Scanner input = new Scanner(System.in);

int userNum;

// Get the User Input

// For loop used to predetermine that width is contained between 3 to 150

for(;;)

{

System.out.print("Enter in a number: ");

userNum = input.nextInt();

if(userNum >= 3 && userNum <= 25)

{

if(userNum%2 == 1)

{

break;

}

else

System.out.println("The number must be 3-25.\nPlease enter the"+ "number again.");

}

else

System.out.println("The number must be 3-25.\nPlease enter the"+ "number again.");

}

// Creation the 'X'

//

for(int row = 1; row <= userNum; row++)

{

System.out.println("");

// The line would be drawn to form the one side of the 'X' exactly when // the the Row's and the Columns having the same axises.

// Same occurs for the other opposite side by negatively.

for(int column = 1; column <= userNum; column++)

{

if(row == column || (row + column) == (userNum + 1))

System.out.print("X");

else

System.out.print(" ");

}

}

}

}

Screenshot of a Typical Run:

Problems Encountered:

In this program, I encountered problem with the condition statement where I need to control the user from entering in the even numbers to avoid the "crowding of the 'X's" problem. Other than that, nothing was an issue.

Debugging Methods Used:

No debugging was used in this program.

References List

STALLINGS, W. (2006) Computer Organization and Architecture. Eighth Edition. New Jersey USA: Pearson Education.

END OF COURSEWORK I

Writing Services

Essay Writing
Service

Find out how the very best essay writing service can help you accomplish more and achieve higher marks today.

Assignment Writing Service

From complicated assignments to tricky tasks, our experts can tackle virtually any question thrown at them.

Dissertation Writing Service

A dissertation (also known as a thesis or research project) is probably the most important piece of work for any student! From full dissertations to individual chapters, we’re on hand to support you.

Coursework Writing Service

Our expert qualified writers can help you get your coursework right first time, every time.

Dissertation Proposal Service

The first step to completing a dissertation is to create a proposal that talks about what you wish to do. Our experts can design suitable methodologies - perfect to help you get started with a dissertation.

Report Writing
Service

Reports for any audience. Perfectly structured, professionally written, and tailored to suit your exact requirements.

Essay Skeleton Answer Service

If you’re just looking for some help to get started on an essay, our outline service provides you with a perfect essay plan.

Marking & Proofreading Service

Not sure if your work is hitting the mark? Struggling to get feedback from your lecturer? Our premium marking service was created just for you - get the feedback you deserve now.

Exam Revision
Service

Exams can be one of the most stressful experiences you’ll ever have! Revision is key, and we’re here to help. With custom created revision notes and exam answers, you’ll never feel underprepared again.