Introduction To Java And Object Oriented Programming English Language Essay

Published: November 21, 2015 Words: 4943

The phrase is put in between the delimiters and so that it will be ignored by the compiler. The effect of enclosing anything between the delimiters is that the compiler will treat every texts enclosed in it as a remark or comment and will ignore them when compiling the file.

'import java.io.*' means that to import all the classes that are in the 'java.io' package to the file. We are importing the classes and their methods from the package to the java application. If 'java.io' had not been imported, we will get a compilation error if we are using a class or method in its package. However, if the program we are running does not have any class or method in the package, nothing will happen and the program will run smoothly.

The purpose of the asterisk ( ) after 'io' is to define all the classes in the package. In the package 'java.io', there are several classes. If the programmer just wants to use a single class from the package, he or she can just state the class file name instead of the asterisk. However, if the programmer wants to use more than one class files, he or she can use the asterisk to state all the classes from the package rather than stating each class file one by one.

A 'class' in java is like a file that consists of information that can construct objects whereas an 'object' is like an item in the class.[2]

The word 'public' means that the method is accessible to every user and can be called from other objects of other type.

The word 'static' means that the method does not have any object state and can be called without providing an object.

The word 'void' means that the method has nothing to return.

The word 'main' refers to the main method which is the starting point of the Java application.

The purpose of a semi-colon is to mark the end of a statement.[1]

The method's name is "println".

Data Types

Whole Numbers:[3]

Name

Bytes of Storage Taken Up

Range

Example

2.1

int

4

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

42

2.2

byte

1

-128 to +127

3

2.3

short

2

-32,768 to +32,767

200

2.4

long

8

-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

586050

Fractions: [3]

Name

Bytes of Storage Taken Up

Number of Significant Digits

Example

2.5

float

4

7

2.3

2.6

double

8

15

123.4

Single Characters: [3]

Name

Bytes of Storage Taken Up

Example

2.7

char

2

B

Logical Outcomes: [3]

Name

Bytes of Storage Taken Up

Example

2.8

boolean

1

true

Variables

A variable is like a container used to store values so that these values can be used or modified in Java.[1]

We can tell the computer what type of data a variable can hold by declaring the data type followed by the names given to the variables which is known as declaration statement.[1]

We will get an error message saying that the data type is incompatible with the assigned variable if we try to assign the wrong data type to a variable.

(a) foreign and (c) purchaselimit are allowed as names for naming a variable because they are not Java reserved words and they do not contain spaces.

(b) native is not allowed as name for naming a variable is because it is a Java reserved word. (d) purchase limit is not allowed as name for naming a variable is because it contain spaces.[1]

Operators

Arithmetic

Operator

Example

+

a = 1+2;

-

b = 5-4;

*

c = 2*3;

/

d = 8/2;

%

e = 7%2;

Assignment

Operator

Example

=

int a = 2;

Relational

Operator

Example

<

x < y

>

x > y

==

x == y

>=

x >= y

<=

x <= y

!=

x != y

Logical

Operator

Example

&&

a && b

||

a || b

Control Structures

Making Choices

5.1.1

if(marks >= 50)

{

System.out.println("You pass!");

}

else

{

System.out.println("You fail!");

}

5.1.2

switch(item)

{

case 1 : price = 2.45;

break;

case 2 : price = 3.50;

break;

case 3 : price = 5.00;

break;

default : price = 0;

}

Doing Something Repeatedly

5.2.1

while(n < 5)

{

System.out.println(n);

n++;

}

5.2.2

do

{

System.out.println(n);

n++;

}

while(n < 5);

5.2.3

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

{

System.out.println("Hello World");

}

Programming Practice

Simple Comparison I

Program Code:

package questionone;

import java.util.*;

public class QuestionOne

{

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

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

System.out.print("First number: ");

int num1 = in.nextInt();

System.out.print("Second number: ");

int num2 = in.nextInt();

// to check if the user inputs the same number as num1,

// if it is the same number as num1, prompt user to

// enter another number

while(num1 == num2)

{

System.out.println("Please enter a different number.");

System.out.print("Second number: ");

num2 = in.nextInt();

}

System.out.print("Third number: ");

int num3 = in.nextInt();

// to check if the user inputs the same number as num1,

// or num2, if it is the same number as num1, prompt user

// to enter another number

while(num3 == num1 || num3 == num2)

{

System.out.println("Please enter a different number.");

System.out.print("Third number: ");

num3 = in.nextInt();

}

// to compare the three numbers, first find out which is

// the smallest, then for the remaining two number, we find

// out which is smaller than the other in the nested if statement

if(num1 < num2 && num1 < num3)

{

System.out.println("\nThe smallest number is: " + num1);

if(num2 < num3)

{

System.out.println("The middle number is: " + num2);

System.out.println("The largest number is: " + num3);

}

else

{

System.out.println("The middle number is: " + num3);

System.out.println("The largest number is: " + num2);

}

}

else if(num2 < num1 && num2 < num3)

{

System.out.println("\nThe smallest number is: " + num2);

if(num1 < num3)

{

System.out.println("The middle number is: " + num1);

System.out.println("The largest number is: " + num3);

}

else

{

System.out.println("The middle number is: " + num3);

System.out.println("The largest number is: " + num1);

}

}

else if(num3 < num1 && num3 < num2)

{

System.out.println("\nThe smallest number is: " + num3);

if(num1 < num2)

{

System.out.println("The middle number is: " + num1);

System.out.println("The largest number is: " + num2);

}

else

{

System.out.println("The middle number is: " + num2);

System.out.println("The largest number is: " + num1);

}

}

}

}

Screenshot of Typical Run:

run1.jpg

Problems Encountered:

The problem that I encountered when writing this program is that I used an "if statement" for catching the same input at first. When I tested the same number for more than a couple of times, the program broke through the "if statement" and went on ahead to ask the user for the third number even though the first and the second number are the same. I changed the "if statement" to a "while loop" to catch the user's same input and it solved the problem.

Testing of Program:

I ensure that the program is working properly by trying to input the same numbers repeatedly. If the catching of the same input is working properly, then I proceed by entering a different number. I repeated this step when the program asks for the third number. After entering all three different numbers, the program should print out the three numbers in ascending order.

Screenshot of Test:

test1.jpg

Simple Comparison II

Program Code:

package questiontwo;

import java.util.*;

public class QuestionTwo

{

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

System.out.println("Enter three alphabets.");

System.out.print("First alphabet: ");

String input1 = in.nextLine();

char alpha1 = input1.charAt(0);

System.out.print("Second alphabet: ");

String input2 = in.nextLine();

char alpha2 = input2.charAt(0);

// to check if the user inputs the same alphabet as alpha1,

// if it is the same number as alpha1, prompt user to

// enter another alphabet

while(alpha1 == alpha2)

{

System.out.println("Please enter a different alphabet.");

System.out.print("Second alphabet: ");

input2 = in.nextLine();

alpha2 = input2.charAt(0);

}

System.out.print("Third alphabet: ");

String input3 = in.nextLine();

char alpha3 = input3.charAt(0);

// to check if the user inputs the same alphabet as alpha1,

// or alpha2, if it is the same number as alpha1, prompt user

// to enter another alphabet

while(alpha3 == alpha1 || alpha3 == alpha2)

{

System.out.println("Please enter a different alphabet.");

System.out.print("Third alphabet: ");

input3 = in.nextLine();

alpha3 = input3.charAt(0);

}

// to compare the three alphabets, first find out which is

// the smallest, then for the remaining two alphabets, we find

// out which is smaller in the nested if statement

if(alpha1 < alpha2 && alpha1 < alpha3)

{

System.out.println("\nThe smallest alphabet is: " + alpha1);

if(alpha2 < alpha3)

{

System.out.println("The middle alphabet is: " + alpha2);

System.out.println("The largest alphabet is: " + alpha3);

}

else

{

System.out.println("The middle alphabet is: " + alpha3);

System.out.println("The largest alphabet is: " + alpha2);

}

}

else if(alpha2 < alpha1 && alpha2 < alpha3)

{

System.out.println("\nThe smallest alphabet is: " + alpha2);

if(alpha1 < alpha3)

{

System.out.println("The middle alphabet is: " + alpha1);

System.out.println("The largest alphabet is: " + alpha3);

}

else

{

System.out.println("The middle alphabet is: " + alpha3);

System.out.println("The largest alphabet is: " + alpha1);

}

}

else if(alpha3 < alpha1 && alpha3 < alpha2)

{

System.out.println("\nThe smallest alphabet is: " + alpha3);

if(alpha1 < alpha2)

{

System.out.println("The middle alphabet is: " + alpha1);

System.out.println("The largest alphabet is: " + alpha2);

}

else

{

System.out.println("The middle alphabet is: " + alpha2);

System.out.println("The largest alphabet is: " + alpha1);

}

}

}

}

Screenshot of Typical Run:

run2.jpg

Problem Encountered:

The problem that I encountered when writing this program is that I did not reassign the "char" variables after the user inputs a different alphabet at first. The program goes on an infinite "while loop" even if the user inputs a different alphabet after that. I reassign the char variables in the "while loop" and it solved the problem.

Testing of Program:

I ensure that the program is working properly by trying to input the same alphabets repeatedly. If the catching of the same input is working properly, then I proceed by entering a different alphabet. I repeated this step when the program asks for the third alphabet. After entering all three different alphabets, the program should print out the three alphabets in ascending order.

Screenshot of Test:

test2.jpg

Simple Comparison III

Program Code:

package questionthree;

import java.util.*;

public class QuestionThree

{

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

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

System.out.print("First character: ");

String input1 = in.nextLine();

char sym1 = input1.charAt(0);

System.out.print("Second character: ");

String input2 = in.nextLine();

char sym2 = input2.charAt(0);

// to check if the user inputs the same character as sym1,

// if it is the same number as sym1, prompt user to

// enter another character

while(sym1 == sym2)

{

System.out.println("Please enter a different character.");

System.out.print("Second character: ");

input2 = in.nextLine();

sym2 = input2.charAt(0);

}

System.out.print("Third character: ");

String input3 = in.nextLine();

char sym3 = input3.charAt(0);

// to check if the user inputs the same character as sym1,

// or sym2, if it is the same number as sym1, prompt user

// to enter another character

while(sym3 == sym1 || sym3 == sym2)

{

System.out.println("Please enter a different character.");

System.out.print("Third character: ");

input3 = in.nextLine();

sym3 = input3.charAt(0);

}

// to compare the three characters, first find out which is

// the smallest, then for the remaining two characters, we find

// out which is smaller in the nested if statement

if(sym1 < sym2 && sym1 < sym3)

{

System.out.println("\nThe smallest character is: " + sym1);

if(sym2 < sym3)

{

System.out.println("The middle character is: " + sym2);

System.out.println("The largest character is: " + sym3);

}

else

{

System.out.println("The middle character is: " + sym3);

System.out.println("The largest character is: " + sym2);

}

}

else if(sym2 < sym1 && sym2 < sym3)

{

System.out.println("\nThe smallest character is: " + sym2);

if(sym1 < sym3)

{

System.out.println("The middle character is: " + sym1);

System.out.println("The largest character is: " + sym3);

}

else

{

System.out.println("The middle character is: " + sym3);

System.out.println("The largest character is: " + sym1);

}

}

else if(sym3 < sym1 && sym3 < sym2)

{

System.out.println("\nThe smallest character is: " + sym3);

if(sym1 < sym2)

{

System.out.println("The middle character is: " + sym1);

System.out.println("The largest character is: " + sym2);

}

else

{

System.out.println("The middle character is: " + sym2);

System.out.println("The largest character is: " + sym1);

}

}

}

}

Screenshot of Typical Run:

run3.jpg

Problems Encountered:

The problem that I encountered when writing this program is that I am not sure that the program rearranges the symbols in the correct ascending order. I went to search for an ASCII Table on the internet and refer to the symbols that I input and compare them manually which helped me in solving my problem.

Testing of Program:

I ensure that the program is working properly by trying to input the same characters repeatedly. If the catching of the same input is working properly, then I proceed by entering a different character. I repeated this step when the program asks for the third character. After entering all three different characters, the program should print out the three characters in ascending order according to their ASCII value.

Screenshot of Test:

test3.jpg

Formatting Output

Program Code:

package questionfour;

public class QuestionFour

{

public static void main(String[] args)

{

double num = 8.0/3;

// a normal print

System.out.println("Eight divided by three is " + num);

// printing 8/3 in different format of widths and precisions

System.out.printf("Eight divided by three is %8.6f%n", num);

System.out.printf("Eight divided by three is %6.4f%n", num);

String name1 = "Alex";

String name2 = "Bobby";

String name3 = "Charles";

System.out.println("\nLeft Justified: ");

// justify 18 spaces to the left

System.out.printf("%-18s%n", name1);

System.out.printf("%-18s%n", name2);

System.out.printf("%-18s%n", name3);

System.out.println("\nRight Justified: ");

// justify 18 spaces to the right

System.out.printf("%18s%n", name1);

System.out.printf("%18s%n", name2);

System.out.printf("%18s%n", name3);

double num2 = 123.456;

double num3 = 1.23;

double num4 = 98.7654;

//lined up on the decimal points of the numbers

System.out.printf("\nLined up on decimal point %7.3f%n", num2);

System.out.printf("Lined up on decimal point %6.2f%n", num3);

System.out.printf("Lined up on decimal point %8.4f%n", num4);

}

}

Screenshot of Typical Run:

run4.jpg

Problems Encountered:

The problem that I encountered when writing this program is that I do not know that there are different converters and flags for different data types. I used a decimal integer converter for a string initially and I was shown an error message. I went to research on the Formatter class and found out that there are different converters and flags which helped me solved the problem.

Testing of Program:

I ensure that the program is working properly by trying different precisions and widths with different type of converters. I have also count every precision and width manually and see whether it tallies with the formatter code.

The Guessing Game

Program Code:

package questionfive;

import java.util.*;

public class QuestionFive

{

public static void main(String[] args)

{

// generate a random number from 1 to 1000

Random randGen = new Random();

int randNo = randGen.nextInt(1000)+1;

boolean bingo = false;

Scanner in = new Scanner(System.in);

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

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

int userNum = in.nextInt();

// if user input is not the correct answer, it will keep

// on performing this check

while(bingo == false)

{

// if user enters a number that is not in range of 1 to 1000

if(userNum < 1 || userNum > 1000)

{

System.out.println("Please enter a number between 1 and 1000.");

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

userNum = in.nextInt();

}

// if user input is higher than the random number

else if(userNum > randNo)

{

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

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

userNum = in.nextInt();

}

// if user input is lower than the random number

else if(userNum < randNo)

{

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

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

userNum = in.nextInt();

}

// if user inputs the correct number and turns boolean to true

else if(userNum == randNo)

{

bingo = true;

}

}

// a feedback when user inputs the correct number

if(bingo == true)

{

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

}

}

}

Screenshot of Typical Run:

run5.jpg

Problems Encountered:

The problem that I encountered when writing this program is that I did not add 1 to the random number at first. This makes the random number have a chance of being 0 and the random number will not have a chance of being 999.

Testing of Program:

I ensure that the program is working properly by printing the random number for testing purpose. I then input different numbers that are bigger and smaller than the random number to check whether the feedback messages are working properly. Lastly, I input the random number and see if it gives me the correct feedback message.

Screenshot of Test:

test5.jpg

Shopping Cart I

Program Code:

main.java

package ShoppingCart;

public class shoppingCart

{

public static void main(String[] args)

{

Shopping app = new Shopping();

app.start();

}

}

class.java

package ShoppingCart;

import java.text.*;

import java.util.*;

public class Shopping

{

Product[] orderProduct = new Product[3];

boolean valid = false;

DecimalFormat decFor = new DecimalFormat("0.00");

public void start()

{

printCatalogue();

shop();

checkout();

}

public void printCatalogue()

{

// creating array of products

orderProduct[0] = new Product("Condensed Powdered Water", "P3487", 2.50, "per packet", 0);

orderProduct[1] = new Product("Distilled Moonbeams", "K3876", 3.00, "a dozen", 0);

orderProduct[2] = new Product("Anti-Gravity Pills", "Z9983", 12.75, "for 60", 0);

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

for(int i=0; i<orderProduct.length; i++)

{

// printing the product informations

System.out.print("(" + (i+1) + ") ");

orderProduct[i].displayProduct();

}

}

public void shop()

{

System.out.println("\nBuy something!");

Scanner in = new Scanner(System.in);

String userOrder;

int userQuantity;

do

{

valid = false;

// user will be prompted to enter a product code

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

userOrder = in.next();

// the user's input will be compared to the array of

// product codes. if there is a match found, Boolean

// will be changed to true and the user will be

// prompted to enter the quantity.

for(int i=0; i<orderProduct.length; i++)

{

if(userOrder.equals(orderProduct[i].code))

{

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

userQuantity = in.nextInt();

// quantity entered will be added on to the amount attribute

orderProduct[i].amount += userQuantity;

valid = true;

break;

}

}

// if there is no match found, boolean remains false

// and will print out error message after the for loop

if(!valid && !userOrder.equals("0"))

{

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

}

}

while(!userOrder.equals("0"));

}

public void checkout()

{

double total;

double subTotal = 0;

double tax;

double grandTotal;

System.out.println("\nYour order:");

for(int i=0; i<orderProduct.length; i++)

{

if(orderProduct[i].amount != 0)

{

// calculation of the cost of each product

total = (orderProduct[i].amount * orderProduct[i].price);

System.out.println(orderProduct[i].amount + " " + orderProduct[i].item + " @ $" + decFor.format(orderProduct[i].price) + " = $" + decFor.format(total));

// cost of each product is added on to the subtotal

subTotal += total;

}

}

// calculation of tax and the grand total

tax = 0.2 * subTotal;

grandTotal = subTotal + tax;

System.out.println("Subtotal: $" + decFor.format(subTotal) + "\tTax @ 20%: $" + decFor.format(tax) + "\tGrand Total: $" + decFor.format(grandTotal));

}

}

class.java (constructor)

package ShoppingCart;

import java.text.*;

public class Product

{

public String item;

public String code;

public String unit;

public double price;

public int amount;

DecimalFormat decFor = new DecimalFormat("0.00");

public Product(String i, String c, double p, String u, int a)

{

item = i;

code = c;

price = p;

unit = u;

amount = a;

}

public void displayProduct()

{

System.out.println(item + " [" + code + "], " + "$" + decFor.format(price) + " " + unit);

}

}

Screenshot of Typical Run:

run6.jpg

Problems Encountered:

The problem that I encountered when writing this program is that I could not think of a way to remember the quantity that the user entered for that particular product code at first. After much planning and thinking, I tried adding another attribute called "amount" in the constructor class. I added the quantity entered by the user to each of the product by identifying them with the product code and it solved my problem.

Testing of Program:

I ensure that the program is working properly by entering wrong inputs and real product code alternately. This is to check if the catching of wrong inputs, the error message and the prompting of entering the quantity are working properly. After entering all three real product codes and the quantities, I would end the program by pressing zero, to see if the program would end and print the calculations out. Lastly, I ensure the calculations are correct by calculating it manually.

Screenshot of Test:

test6.jpg

Shopping Cart II

Program Code:

main.java

package ShoppingCart;

public class shoppingCart

{

public static void main(String[] args)

{

Shopping app = new Shopping();

app.start();

}

}

class.java

package ShoppingCart;

import java.text.*;

import java.util.*;

public class Shopping

{

Product[] orderProduct = new Product[3];

boolean valid = false;

DecimalFormat decFor = new DecimalFormat("0.00");

public void start()

{

printCatalogue();

shop();

checkout();

}

public void printCatalogue()

{

// creating array of products

orderProduct[0] = new Product("Condensed Powdered Water", "P3487", 2.50, "per packet", 0);

orderProduct[1] = new Product("Distilled Moonbeams", "K3876", 3.00, "a dozen", 0);

orderProduct[2] = new Product("Anti-Gravity Pills", "Z9983", 12.75, "for 60", 0);

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

for(int i=0; i<orderProduct.length; i++)

{

// printing the product informations

System.out.print("(" + (i+1) + ") ");

orderProduct[i].displayProduct();

}

}

public void shop()

{

System.out.println("\nBuy something!");

Scanner in = new Scanner(System.in);

String userOrder;

int userQuantity;

do

{

valid = false;

// user will be prompted to enter a product code

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

userOrder = in.next();

// the user's input will be compared to the array of

// product codes. if there is a match found, Boolean

// will be changed to true and the user will be

// prompted to enter the quantity.

for(int i=0; i<orderProduct.length; i++)

{

if(userOrder.equals(orderProduct[i].code))

{

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

userQuantity = in.nextInt();

// if the quantity input is between 0 and 100,

// it will be added on to the amount attribute

if(userQuantity > 0 && userQuantity < 100)

{

orderProduct[i].amount += userQuantity;

}

// if the quantity input if more than 99, user

// will be prompted to enter the quantity a

// second time.

else if(userQuantity > 99)

{

System.out.print("You are ordering a large quantity.\nPlease enter quantity again to confirm: ");

userQuantity = in.nextInt();

orderProduct[i].amount += userQuantity;

}

// if the quantity input is less than 1, it

// will be rejected

else if(userQuantity < 1)

{

System.out.println("Sorry. Cannot accept quantity less than 1.");

}

valid = true;

break;

}

}

// if there is no match found, boolean remains false and

// will print out error message depending on the input

if(!valid && !userOrder.equals("0") && Character.isLetter(userOrder.charAt(0)))

{

// if the first character of the input is a letter

// and is not 5 characters unlike a product code, print

// the normal error message

if(userOrder.length() != 5)

{

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

}

else

{

// if the first character of the input is a letter

// and is 5 characters like a product code, check

// the subsequent 4 character

for(int i=1; i<5; i++)

{

if(!Character.isDigit(userOrder.charAt(i)))

{

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

break;

}

// if 4 of them are digits, print the product code

// error message

if(i == 4)

{

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

}

}

}

}

else if(!valid && !userOrder.equals("0"))

{

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

}

}

while(!userOrder.equals("0"));

}

public void checkout()

{

double total;

double subTotal = 0;

double tax;

double grandTotal;

System.out.println("\nYour order:");

for(int i=0; i<orderProduct.length; i++)

{

if(orderProduct[i].amount != 0)

{

// calculation of the cost of each product

total = (orderProduct[i].amount * orderProduct[i].price);

System.out.println(orderProduct[i].amount + " " + orderProduct[i].item + " @ $" + decFor.format(orderProduct[i].price) + " = $" + decFor.format(total));

// cost of each product is added on to the subtotal

subTotal += total;

}

}

// calculation of tax and the grand total

tax = 0.2 * subTotal;

grandTotal = subTotal + tax;

System.out.println("Subtotal: $" + decFor.format(subTotal) + "\tTax @ 20%: $" + decFor.format(tax) + "\tGrand Total: $" + decFor.format(grandTotal));

}

}

class.java (constructor)

package ShoppingCart;

import java.text.*;

public class Product

{

public String item;

public String code;

public String unit;

public double price;

public int amount;

DecimalFormat decFor = new DecimalFormat("0.00");

public Product(String i, String c, double p, String u, int a)

{

item = i;

code = c;

price = p;

unit = u;

amount = a;

}

public void displayProduct()

{

System.out.println(item + " [" + code + "], " + "$" + decFor.format(price) + " " + unit);

}

}

Screenshot of Typical Run:

run7.jpg

Problems Encountered:

The problem that I encountered when writing this program is the checking of the non-existing product code. After checking that the first character is a letter, the program will go on and check whether the subsequent four characters are digits. However, it will print four times "I don't recognize that Product Code" error message. I added a "if statement" whereby if the for loop is at the last character and has been confirmed that it is a digit, then print the error message. Since it is the last character, the program will break out of the "for loop" resulting in printing the error message only once.

Testing of Program:

For the quantity input, I ensure that the program is working properly by entering impossible quantity such as negative numbers and zero to see if the program will reject them. Next, I try entering a number more than 100 to check whether the program will prompt me to enter the quantity again. For the checking of non-existing product code, I try different kind of inputs that consists of a letter at the first character and digits for the subsequent four characters. For example like "P1234", "T2345" and so on. I have also tried other wrong inputs to test whether the catching of normal wrong inputs and its error message are still working properly.

Screenshot of Test:

test7.jpg

Loops Within Loops I

Program Code:

package questioneight;

public class QuestionEight

{

public static void main(String[] args)

{

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

{

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

{

// print X if column is less than 3 or more than 5

// print X if row is 3 also

if(j<3 || j>5 || i==3)

{

System.out.print("X");

}

else

{

System.out.print(" ");

}

}

System.out.println();

}

}

}

Screenshot of Typical Run:

run8.jpg

Problems Encountered:

The problem that I encountered when writing this program is that I have difficulty trying to print the middle part of the 'H' shape at first. In the end, I have found out that by adding the condition, "i" is equals to three in the "if statement" it will still print if I use the || (or) operation.

Testing of Program:

I ensure that the program is working properly by testing out other different width and length of the letter 'H' to see if it prints out in the correct manner.

Screenshot of Test

test8.jpg

Loops Within Loops II

Program Code:

package questionnine;

public class QuestionNine

{

public static void main(String[] args)

{

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

{

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

{

// print X in a shape of a back slash

// and a forward slash

if(i == j || (i+j == 4))

{

System.out.print("X");

}

else

{

System.out.print(" ");

}

}

System.out.println();

}

}

}

Screenshot of Run:

run9.jpg

Problems Encountered:

The problem that I encountered when writing this program is that I have difficulty coming out with the conditions for the "if statement" so that the program will print 'X' and space at the exact place to form the letter 'X' shape. I keep on coming out with conditions until I tried until a point where I have a backwards slash of the letter 'X', which is when "i" is equals to "j". I found out that to create a forward slash of the letter 'X', I will just need to find out where "i" and "j" will add together to be the width and it solved the problem.

Testing of Program:

I ensure that the program is working properly by changing the width of the letter 'X' to other odd number values to see if it prints out in the correct manner.

Screenshot of Test

test9.jpg

Loops Within Loops III

Program Code:

package questionten;

import java.util.Scanner;

public class QuestionTen

{

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

boolean valid = false;

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

int width = in.nextInt();

while(!valid)

{

// if user input is more than one and is an

// odd number then print if not reject and

// prompt the user for another input

if(width > 1 && width%2 == 1)

{

for(int i=0; i<width; i++)

{

for(int j=0; j<width; j++)

{

// print X in a shape of a back slash

// and a forward slash

if(i == j || (i+j == width-1))

{

System.out.print("X");

}

else

{

System.out.print(" ");

}

}

System.out.println();

}

valid = true;

}

else

{

System.out.println("Please enter a odd number width.");

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

width = in.nextInt();

}

}

}

}

Screenshot of Run:

run10.jpg

Problems Encountered:

The problem that I encountered when writing this program is that I only reject odd numbers input at first. I found the problem when I entered in zero and negative numbers, there is no printing of 'X' at all. I added in an additional condition that only accepts numbers larger than one to the "if statement" and it solved the problem.

Testing of Program:

I ensure that the program is working properly by entering numbers that are impossible to form a perfect 'X' shape such as even numbers, negative numbers and zero.

Screenshot of Test:

test10.jpg