67
Mark L. Simon II Individual Project / IP 1 thru 5 IT110 -1404A-01 Introduction to Programming Professor: Mazen Alkhatib 10 November, 2014 Colorado Technical University

Introduction To Programming IP5

Embed Size (px)

Citation preview

Page 1: Introduction To Programming IP5

Mark L. Simon II

Individual Project / IP 1 thru 5

IT110 -1404A-01

Introduction to Programming

Professor: Mazen Alkhatib

10 November, 2014

Colorado Technical University

Page 2: Introduction To Programming IP5

PROGRAMMING 2

Table of ContentsChapter One – Fundmentals............................................................................................................3

Overview......................................................................................................................................3

Java Fundamentals.......................................................................................................................4

Re-Factored Java Code................................................................................................................5

Java Runtime Environment..........................................................................................................6

Conclusion...................................................................................................................................9

Chapter 2 - Java Control Structures...............................................................................................10

Control Structures......................................................................................................................10

Script for Method.......................................................................................................................10

Splash Pages for Program..........................................................................................................12

Conclusion.................................................................................................................................14

Chapter 3 - Error Handling and File Input or Output....................................................................15

Errors and Exceptions................................................................................................................15

Script for Input / Output, Error and Exception Handling..........................................................16

Conclusion.................................................................................................................................23

Chapter 4 - Object-Oriented Techniques.......................................................................................24

Object-Orientated Class Script..................................................................................................25

The Output.................................................................................................................................31

Conclusion.................................................................................................................................36

Chapter 5 - Inheritance and Polymorphism...................................................................................37

Arrays and Sub Classes.............................................................................................................37

Virtual Output............................................................................................................................44

Screen Shots of Operation with Arrays.........................................................................................47

Conclusion.................................................................................................................................52

References......................................................................................................................................52

Page 3: Introduction To Programming IP5

PROGRAMMING 3

Programing IT110

Chapter One – Fundmentals

Overview

Welcome to the world of beginners programming where you can lose your mind crunching the

script or letting the developer program do it for you. In this phase, I was given a few lines of

script to design my own opening statement or splash page on a web site. My website entails

selling computer hardware components at reduced prices. The primary purpose of this website

welcome splash is to aid homeowners in getting items for their computers without paying the

exorbitant prices normally seen with computer building. In the website the customer has the

choice of various components to choose from. There will be mobos, processors, heat sinks,

RAM, graphics cards, hard drives, cases, and monitors. In a use case scenario the graph will

look as seen below in figure1-1.

(Figure 1-1)

Page 4: Introduction To Programming IP5

PROGRAMMING 4

Java Fundamentals

I had a decision to make and that decision was weather Eclipse or NetBeans would be used to

formulate this program. I initiated Eclipse and tried integrating everything into one program. I

quickly found that Eclipse was not very user friendly for the first time user. It would tell me

what was wrong, but wasn’t able to quickly rectify mistakes for me, or seem to want to debug

each of the steps responsible for the script halting and giving errors. I threw the file all out, shut

down Eclipse and started the NetBeans console. This program easily started a new page, could

quickly integrate the PizzaRUS.java file in without delay and it was easy to do the fundamental

integration. Once they were both on the NetBeans board, the console quickly told me the

problems; where they were at, and possibilities for fixes by the quick click of a button. Once the

debugger was run and the j-options problem was worked, it all came together to run and create

the windows pop-ups needed for introduction. Below is the refactored code, which made it all

happen.

Re-Factored Java Code

/*IT110-1404A-01 (Introduction to Programming)

* Claw Concepts Phase 1 IP

* Created on 10/09/14 by Mark L. Simon II

* Instructor : Mazen Alkhatib

*/

package ClawConceptsPhase1;

import javax.swing.JOptionPane;

/**

*

* @author Mark Simon

*/

public class Claw_Concepts {

Page 5: Introduction To Programming IP5

PROGRAMMING 5

public static void main(String[] args){

// declare variables

String openingMsg, nameInputMsg, customerName, nameOutputMsg,

returnInputMsg, customerReturn, returnOutputMsg,

greetingOutputMsg, outputMsg;

// display opening message

openingMsg = "*** Welcome to Claw Concepts Ordering System ***\n"

+ " Shop for the part you desire!";

JOptionPane.showMessageDialog(null, openingMsg);

// get required input using dialogs

nameInputMsg = "Please enter your name: ";

customerName = JOptionPane.showInputDialog(nameInputMsg);

returnInputMsg = "Are you a returning customer (yes or no)? ";

customerReturn = JOptionPane.showInputDialog(returnInputMsg);

// build output strings

nameOutputMsg = "Welcome " + customerName + ".\n\n";

returnOutputMsg = "Your return customer status is " + customerReturn + ".\n";

greetingOutputMsg = "Thank you for visiting Claw Concepts!" + "\n\n"

+ "Your order will be shipped within the next 24 hours.\n";

// create and display output string

outputMsg = nameOutputMsg + returnOutputMsg + greetingOutputMsg;

JOptionPane.showMessageDialog(null, outputMsg);

System.exit(0);

} // end main()

} // end class ClawConceptsPhase1

(Figure 1-2)

Page 6: Introduction To Programming IP5

PROGRAMMING 6

Java Runtime Environment

As I ran the program the following windows started popping up. I placed the required

information into each of the windows to ensure it would take the information and change to the

next function and then ran it again without placing information in to the boxes. The program

still sequenced through the motions and then ended the function.

(Figure 1-3)

As we see above on (figure 1-3), the first splash screen represents “Claw Concepts Ordering

System”. As soon as “OK” is hit the next splash screen appears, as is seen in (figure 1-4).(Figure 1-4)

Page 7: Introduction To Programming IP5

PROGRAMMING 7

As we see in (figure 1-4), I am able to easily place my name in the box for temporary

verification of customer. Once the “OK button is clicked the next splash window pops up as is

seen in (figure 1-5).

(Figure 1-5)

In figure 1-5 it represents the question to the customer if they are a reoccurring customer.

I have typed “Yes I am” in the box to see if it will annotate the process. Once this is done, I hit

the “OK” button to move on through the program. What is observed next is in (figure 1-6).

(Figure 1-6)

Page 8: Introduction To Programming IP5

PROGRAMMING 8

As we can see in (figure 1-6), my name and the customer status have exactly what I

entered into the prior boxes. The run test is now complete.

Conclusion

For the phase one assignment we were asked to integrate a program into Java NetBeans

or Eclipse to make it a splash screen questionnaire for our web site. Mine was exhibited as

“Claw Concepts” and integrated into a form of Java pseudocode with the steps needed to execute

the program. Upon completion of the program and pulling out un-needed lines of script, the file

was capable of initiating and presenting screen shots of its execution. Refactoring was a key

concept in ensuring proper function and execution of the program, with the help of the

debugging tools within the program. In the end all functions executed for the needed operation.

Page 9: Introduction To Programming IP5

PROGRAMMING 9

Chapter 2 - Java Control Structures

Control Structures

Control structures are the very essence of what makes a Java program run without

sequentially stating each line within a given program. By using decision making statements,

control statements, looping statements, branching statements, break statements, and return

statements; a program can choose which parameters are needed given the equation it’s chewing

on. In this portion of the individual project, we have taken the opening “JOptionPane” area and

integrated it with the “getStringInput” scenarios within the JOptionPane program. The result of

this incorporation is to output direct customer needs based on the input from the customer. This

is then all displayed within the splash panes. As we see below (Figure 2-1), the initial program

was scribed into the (CC Method) script.

(Figure 2-1)

Script for Method

import javax.swing.JOptionPane;

public class CC_method_review{

private static String getStringInput(String prompt) { String returnprompt=null;

for (int i =1; i <= 3; i++) {returnprompt = JOptionPane.showInputDialog(prompt); if(returnprompt.isEmpty()) {JOptionPane.showMessageDialog(null, "Invalid Entry!!"); if (i == 3) System.exit(0); } else break; }

Page 10: Introduction To Programming IP5

PROGRAMMING 10

return returnprompt; }

private static double totalCost(int InumberItems, double costPerItem, double discount, double taxRate)

{double Total_Cost; Total_Cost = (InumberItems*costPerItem-InumberItems*costPerItem*discount +InumberItems*costPerItem*taxRate); return Total_Cost; } public static void main(String[] args){ String openingMsg; String prompt,customerName,customerReturn,numberItems,modelType; int InumberItems;

double costPerItem = 25.00,discount = 0.05,taxRate = 0.08,totalCost ;

openingMsg = "Welcome to Claw Concepts"+"\n" + "Shop for the part of your desire!"; JOptionPane.showMessageDialog(null, openingMsg);

prompt = "What's Your Name Please?";

customerName = getStringInput(prompt);

prompt = "Are you a returning Customer?" + "\n" + "Please Enter (Yes or No)";

customerReturn = getStringInput(prompt);

prompt = "How many CD Players would you like?"; numberItems = getStringInput(prompt); InumberItems = Integer.parseInt(numberItems);

prompt = " Which Brand would you like? " + "\n" + " Please Enter Model " + "\n";

modelType = getStringInput(prompt);

Page 11: Introduction To Programming IP5

PROGRAMMING 11

totalCost = totalCost(InumberItems,costPerItem,discount,taxRate);

JOptionPane.showMessageDialog(null,"Thank You For Your Order! " + " Mr. "+ customerName + "\n" + "Your Return Status is = " +customerReturn+ "\n" + "Model Ordered is = " + modelType + "\n" + "Discount = " + discount+"%" +"\n" + "Tax Rate = $" + taxRate+"%" +"\n" + "Total Cost = $"+ totalCost +" \n" + "Your order will be shipped in 24 hours"+"\n"); }}

Splash Pages for Program

Once this program is run the following results were returned.

Missed type-in one time With Name Entered

Next Run yes/ no with nothing entered. Return with Invalid Entry.

Page 12: Introduction To Programming IP5

PROGRAMMING 12

Run with Info Entered. Program continues on. How many CD’s with no entry.

Returns the following “Invalid Entry” With CD Amount entered continues on.

Returns the next splash frame. When nothing is entered for brand/model.

Page 13: Introduction To Programming IP5

PROGRAMMING 13

Now with the brand/model entered. The final outcomes after all entries are

completed.

Conclusion

As has been seen, once the “JOptionPane” is integrated with the “getStringInput” and then

couple with the “showMessageDialog”, we can produce a program with loops, input and

output to work with customers on a web page.

Page 14: Introduction To Programming IP5

PROGRAMMING 14

Chapter 3 - Error Handling and File Input or Output

Errors and Exceptions

Errors are the formidable adversary to any programmers operation. If one period, semi-

colon or any other character is missed or out of sequence, it can mean hours of back tracking to

find this program stopper. Most functions within the program start with identifying variables.

These are then computed into the main function of the program and a final output is returned

using the display options. Seems quite simple, but as these functions start to rely on one another

for progression of the program, road bumps and pot holes will occur, especially if one variable is

not recognized within another function or sub routine of the program. This is where error

handling tool steps into action and lets the programmer know that a certain line of script will not

function until these errors are dealt with. During this phase, it has been noted that error handling

is paramount to the programs capability of running properly.

A built in error handling tool could be called like the Input/output Exception handler.

This may be inserted to a function to create an exception for the routine, if it is found not to be

true under the Boolean concept or other function. It’s an easy way for the function to be cleared

and an error averted if it doesn’t come out with the correct outcome. The exception handler can

do the job, but it must be written correctly or this function can end up being part of the known

errors.

Listed below is this week’s script for showing error situation and how exception handlers

divert problem areas from throwing errors. Now it just throws exceptions and evaluates it

instead of throwing an immediate error. Review in fig 3-1.

Page 15: Introduction To Programming IP5

PROGRAMMING 15

Script for Input / Output, Error and Exception Handling

( Fig 3-1 )package CC_input_output;

/*IT110-1404A-01 (Introduction to Programming) * Claw Concepts Phase 3 IP * Created on 10/21/14 by Mark L. Simon II * Instructor : Mazen Alkhatib */

import java.io.File;import java.io.FileWriter;import java.io.FileReader;import java.io.IOException;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.swing.JOptionPane;/** * @author Mark */public class cc_input_output{

private static String getStringInput(String prompt) {String returnprompt = null; for (int i =1; i <= 3; i++) {returnprompt = JOptionPane.showInputDialog(prompt); if(returnprompt.isEmpty()) {JOptionPane.showMessageDialog(null, "!!!Invalid Entry BUDDY BOY!!!" + "\n" + " Try Again " +"\n"); if (i == 3) System.exit(0);} else break; } return returnprompt; }

public static int getNumericInput(int QN) {boolean er; er = false; String tQN; for(int i=1; i<=3; i++) { try{

Page 16: Introduction To Programming IP5

PROGRAMMING 16

if (QN<1 || QN>99) { er = true; throw new IllegalArgumentException(); } else er = false; } catch (IllegalArgumentException e) { JOptionPane.showMessageDialog( null," Invalid Entry BUDDY BOY!!! ","

ERROR!!", JOptionPane.ERROR_MESSAGE); if (i == 3)System.exit(0); } if (er == false) break; else {tQN = JOptionPane.showInputDialog( "Please enter quantity" ); QN = Integer.parseInt(tQN); } } JOptionPane.showMessageDialog(null, "Valid Quantity = " + QN); return QN; } public static String VLDNO() {String phn = null; Pattern pattern = Pattern.compile("\\d{3}\\-\\s{0,1}\\d{3}\\-\\d{4}"); for(int i=1; i<=3; i++){ phn = JOptionPane.showInputDialog("Please Enter Phone Number in This

Format:###-###-####"); Matcher matcher = pattern.matcher(phn); if (matcher.matches()) {JOptionPane.showMessageDialog(null, "Your Phone Number is Valid"); break; } else {JOptionPane.showMessageDialog(null, "WRONG Format DUDE!!! Try Again!!!" + "\n" + " This time try" + " ###-###-####" + "\n" , "ERROR",JOptionPane.ERROR_MESSAGE); if (i == 3) System.exit(0);} } return phn;

Page 17: Introduction To Programming IP5

PROGRAMMING 17

} private static double totalCost(int InumberItems,double costPerItem, double taxRate) {double Total_Cost; Total_Cost = (InumberItems*costPerItem+InumberItems*costPerItem*taxRate); return Total_Cost; }

@SuppressWarnings({"CallToPrintStackTrace", "empty-statement"}) public static void main(String[] args){ String openingMsg; String prompt,customerName,numberItems,phn,finalMessage; int InumberItems,Quantity; double costPerItem = 5.00,taxRate = 0.08,totalCost; openingMsg = "Welcome to Claw Concepts"+"\n" + "Shop for the part of your desire!"; JOptionPane.showMessageDialog(null, openingMsg); prompt = "What's Your Name Please?"; customerName = getStringInput(prompt); prompt = "How many componets would you like?"; numberItems = getStringInput(prompt); InumberItems = Integer.parseInt(numberItems); Quantity = getNumericInput(InumberItems); phn=VLDNO(); totalCost = totalCost(Quantity,costPerItem,taxRate); finalMessage = "Thank You For Your Order! " + customerName +"\n\n" + "Quantity entered is confirmed as: " + "[ " + Quantity + " ]" + "\n\n" + "Your (phone nimber) is confirmed as: " + "[" + phn + "]" + "\n\n" + "Tax Rate = $ " + taxRate+"%" +"\n" + "Total Cost of purchase = " + String.format("$ %.2f",totalCost) + "\n\

n" + "Your order will be shipped within 24 hours." + "\n"; try{ File file = new File("order.txt");

Page 18: Introduction To Programming IP5

PROGRAMMING 18

FileWriter fileWriter; fileWriter = new FileWriter(file); fileWriter.write("Hello: \n**************** Thank You for "); fileWriter.write("Your Order****************.\n"); fileWriter.write("Your Name Is: " + customerName + "\n"); fileWriter.write("Your Quantity Is: "+"[" + Quantity + "]" + "\n"); fileWriter.write("Your Phone Number Is: "+ phn + "\n"); fileWriter.write("Your Tax rate Is: " + taxRate + "\n"); fileWriter.write("Your Total Cost is: " + String.format("$ %.2f",totalCost) + "\

n"); fileWriter.write("( Your order will be shipped within 24 hours. )"+"\n"); fileWriter.flush(); fileWriter.close(); } catch (IOException e) {e.printStackTrace();} try {

File file = new File("order.txt");FileReader fileReader;

fileReader = new FileReader(file);StringBuffer stringBuffer;

stringBuffer= new StringBuffer();int numCharsRead;char[] charArray = new char[2048];while ((numCharsRead = fileReader.read(charArray)) > 0) {

stringBuffer.append(charArray, 0, numCharsRead);}fileReader.close();System.out.println("Order Display:\n\n ");System.out.println(stringBuffer.toString());

} catch (IOException e) {

e.printStackTrace();} JOptionPane.showMessageDialog(null,finalMessage); }}

As we see above. There can be many areas where errors may pop up in the Try / Catch areas,

but it also develops the exception to handle a possible error of the function. Below is what is

observed when everything runs as planned.

Page 19: Introduction To Programming IP5

PROGRAMMING 19

When the program is started we will see the following panes.

Here is the starting splash page.

It purports to what we have

done in the method area from

before.

The next splash pane asks for

the name. What if we hit the

key before putting in our

name?

The program happily tells

good ole BUDDY BOY that

the input is incorrect and the

user needs to try it again

This time with the name

entered, we will get the

response in the next frame.

Okay…we are on to the next

frame. Here too, if the

information is entered

incorrectly, a warning will pop

up as described in the next

Page 20: Introduction To Programming IP5

PROGRAMMING 20

frame.

It’s the same error as the name,

but this time if no entry, the

number “0” or anything above

“99 is entered, it will come

back as an Error. Here, there

was nothing entered.

In this frame a number was

entered, but it was outside the

(1 through 99) range of the

proper selection parameter.

Once a proper number, from (1

to 99) is entered the next screen

will appear.

Now we have a valid quantity

and the program moves on.

Page 21: Introduction To Programming IP5

PROGRAMMING 21

Now the phone number is

needed. If it is put in wrong or

nothing at all, the following

message will occur.

The DUDE is at it again. All

thumbs and no fingers and

accidently his the return before

entering the phone number.

Now the number is entered

properly. This should return

proper valid compliance in the

next frame.

If we look back at the script,

we can see this as the last input

function before the program

rights it all to file and the

generates a text document

In the package area, we can see

that it has built an order.txt file

saying that it has been written

to a file. Within the file is the

order information needed to fill

the order

Page 22: Introduction To Programming IP5

PROGRAMMING 22

As you are nor reading this, so

is the customer, on the return

splash pane below.

This is what the customer will

see when they are complete on

their order.

Conclusion

Error checking takes on many forms. Weather it is the lines of code or in the function

that is being run. The error and exception handlers are always ensuring everything has to be

entered in a specific way. This is to ensure proper flawless operation of the program and the

data entered into the program. As has been stated through this phase, error and exception

handling are great tools to ensure accuracy of outcome but it’s wise to keep discernment

when deploying their usages.

Page 23: Introduction To Programming IP5

PROGRAMMING 23

Chapter 4 - Object-Oriented Techniques

//CC Classes

[Claw Online Ordering | Department:String |Brand:string; Model:String; Color:String;Size:String|getDepartment();getBrand();getModel();getColor()get Size

(Figure 4-1)

If we want to add our own classes, also known as “Object-Orientated Design”, we have

to create a class diagram to represent the intended idea for our program. Above, in (figure 4-

1), a representation of the intended classes is illustrated to be able to formulate a structure for

the given idea of the classes to be represented. This is then applied to the script to attain a

functional class system within the program. As is represented below (Figure 4-2), the class

Page 24: Introduction To Programming IP5

PROGRAMMING 24

section is designated at the top of the programming. To initiate the various classes entered

within the program. Let’s take a look the script to evaluate how this all works.

Object-Orientated Class Script

(Figure 4-2)/*IT110-1404A-01 (Introduction to Programming) * Claw Concepts Phase 3 IP * Created on 10/21/14 by Mark L. Simon II * Instructor : Mazen Alkhatib */

import java.io.File;import java.io.FileWriter;import java.io.FileReader;import java.io.IOException;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.swing.JOptionPane;/** * @author Mark */ class Parts{ String Brand,model,color,size;

String getSection(String Section){ return Section;}

String getBrand(String Brand){ return Brand;}

String getModel(String Model){ return Model;}

String getColor(String Color){ return Color;}

Page 25: Introduction To Programming IP5

PROGRAMMING 25

String getSize(String Size){ {return Size;} } }public class claw_classes{

private static String getStringInput(String prompt) {String returnprompt = null;

for (int i =1; i <= 3; i++) {returnprompt = JOptionPane.showInputDialog(prompt); if(returnprompt.isEmpty()) {JOptionPane.showMessageDialog(null, "!!!Invalid Entry BUDDY BOY!!!" + "\n" + " Try Again " +"\n"); if (i == 3) System.exit(0);} else break; } return returnprompt; }

public static int getNumericInput(int QN) {boolean er; er = false; String tQN; for(int i=1; i<=3; i++) { try{ if (QN<1 || QN>99) { er = true; throw new IllegalArgumentException(); } else er = false; } catch (IllegalArgumentException e) {

Page 26: Introduction To Programming IP5

PROGRAMMING 26

JOptionPane.showMessageDialog( null," Invalid Entry BUDDY BOY!!! "," ERROR!!", JOptionPane.ERROR_MESSAGE); if (i == 3)System.exit(0); }

if (er == false) break; else {tQN = JOptionPane.showInputDialog( "Please enter quantity" ); QN = Integer.parseInt(tQN); } } JOptionPane.showMessageDialog(null, "Valid Quantity = " + QN); return QN; }

public static String VLDNO() {String phn = null; Pattern pattern = Pattern.compile("\\d{3}\\-\\s{0,1}\\d{3}\\-\\d{4}"); for(int i=1; i<=3; i++){ phn = JOptionPane.showInputDialog("Please Enter Phone Number in This Format:###-###-####");

Matcher matcher = pattern.matcher(phn); if (matcher.matches()) {JOptionPane.showMessageDialog(null, "Your Phone Number is Valid"); break; } else {JOptionPane.showMessageDialog(null, "WRONG Format DUDE!!! Try Again!!!" + "\n" + " This time try" + " ###-###-####" + "\n" , "ERROR",JOptionPane.ERROR_MESSAGE); if (i == 3) System.exit(0);} } return phn; }

Page 27: Introduction To Programming IP5

PROGRAMMING 27

private static double totalCost(int InumberItems,double costPerItem,double taxRate)

{double Total_Cost; Total_Cost = (InumberItems*costPerItem+InumberItems*costPerItem*taxRate); return Total_Cost; }

@SuppressWarnings({"CallToPrintStackTrace", "empty-statement"}) public static void main(String[] args){ String openingMsg; String prompt,customerName,numberItems,phn,finalMessage; String productSection, productBrand, productModel, productColor, productSize; int InumberItems,Quantity; double costPerItem = 25.00,taxRate = 0.08,totalCost;

openingMsg = "Welcome to Claw Concepts"+"\n" + "Shop for the part of your desire!"; JOptionPane.showMessageDialog(null, openingMsg);

prompt = "What's Your Name Please?"; customerName = getStringInput(prompt);

prompt = "What section would you like today? : " + "\n" + "Cases" +"\n" + "Mother Boards" + "\n" + "Graphics" + "\n" + "Monitors" + "\n" + "Hard Drives" + "\n" + "Processors" + "\n" + "RAM" + "\n" + "Optical Drives" +"\n"; productSection = getStringInput(prompt); Parts section = new Parts(); JOptionPane.showMessageDialog(null,"Section selected is = " + section.getSection(productSection));

Page 28: Introduction To Programming IP5

PROGRAMMING 28

prompt = "What brand will you be ordering today? : " ; productBrand = getStringInput(prompt); Parts parts1 = new Parts(); JOptionPane.showMessageDialog(null,"Your Brand selected is : " + parts1.getBrand(productBrand));

prompt = "Which model would you like ? "; productModel = getStringInput(prompt); Parts models = new Parts(); JOptionPane.showMessageDialog(null,"Your Model selected is : " + models.getModel(productModel));

prompt = "What color option would you like? "; productColor = getStringInput(prompt); Parts colors = new Parts(); JOptionPane.showMessageDialog(null,"Your Color selected is : " + colors.getColor(productColor));

prompt = "Please enter Size? " + "\n" + "If size not needed. ENTER: N/A "; productSize = getStringInput(prompt); Parts sizes = new Parts(); JOptionPane.showMessageDialog(null,"Your Size selected is : " + sizes.getSize(productSize));

prompt = "Enter desired [ Quantity ] for your order, please. "; numberItems = getStringInput(prompt); InumberItems = Integer.parseInt(numberItems); Quantity = getNumericInput(InumberItems); phn=VLDNO();

totalCost = totalCost(InumberItems,costPerItem,taxRate);

finalMessage = "Thank You For Your Order! " + customerName +"\n\n" + "You ordered the following item: - " + productBrand + "\n" + "Product Model : " + productModel + "\n " + "Product Color : " + productColor + "\n" + "Product Size : " + productSize + "\n" + "Quantity entered is confirmed as : " + "[ " + Quantity + " ]"

Page 29: Introduction To Programming IP5

PROGRAMMING 29

+ "\n\n" + "Your (phone nimber) is confirmed as: " + "[ " + phn + " ]" + "\n\n" + "Tax Rate = $ " + taxRate+"%" +"\n" + "Total Cost of purchase = " + String.format("$ %.2f",totalCost) + "\n\n" + "Your order will be shipped within 24 hours." + "\n";

try{ File file = new File("order.txt"); FileWriter fileWriter; fileWriter = new FileWriter(file); fileWriter.write("Hello: \n**** Thank You for "); fileWriter.write("Your Order****.\n"); fileWriter.write("Your Name Is: " + customerName + "\n"); fileWriter.write("You ordered the following item: " + productBrand + "\n"); fileWriter.write("Your Model Is: " + productModel + "\n"); fileWriter.write("Your Color Is: " + productColor + "\n"); fileWriter.write("Your item size Is: " + productSize + "\n"); fileWriter.write("Your Quantity Is: " + "[" + Quantity + "]" + "\n"); fileWriter.write("Your Phone Number Is: "+ phn + "\n"); fileWriter.write("Your Tax rate Is: " + taxRate + "\n"); fileWriter.write("Your Total Cost is: " + String.format("$ %.2f",totalCost) + "\n"); fileWriter.write("( Your order will be shipped within 24 hours. )"+"\n"); fileWriter.flush(); fileWriter.close(); } catch (IOException e) {e.printStackTrace();}

try {File file = new File("order.txt");FileReader fileReader;

fileReader = new FileReader(file);StringBuffer stringBuffer;

stringBuffer= new StringBuffer();

Page 30: Introduction To Programming IP5

PROGRAMMING 30

int numCharsRead;char[] charArray = new char[2048];while ((numCharsRead = fileReader.read(charArray)) > 0) {

stringBuffer.append(charArray, 0, numCharsRead);}fileReader.close();System.out.println("Order Display:\n\n ");System.out.println(stringBuffer.toString());

}catch (IOException e) {

e.printStackTrace();} JOptionPane.showMessageDialog(null,finalMessage); }}

The highlighted areas represent the additional script needed to attain the functions the

program needs to attain the extra customer interaction.

The Output

In this section we will see the class parts as they have been highlighted within the script

area. When the program is started the following splash screens will begin to appear.

The “Welcome Screen”.

Page 31: Introduction To Programming IP5

PROGRAMMING 31

First, is to put you name in. If no

name is entered the next pane will

occur.

This will now pop up on any of the

areas that an input is required. For the

purposes of this assignment length, I

will refrain from putting any further

“Invalid Entries”

The normal “Name” input.

This has now been added to guide the

user to the specific area for ordering

their item or product.

One more “Invalid Entry to show that

the invalid entry does work on the

new areas.

Page 32: Introduction To Programming IP5

PROGRAMMING 32

We are now going to the “Case”

section.

The program validates the user’s

request for the selected area.

Now we have the brand to be ordered.

A Cooler Master case seems to be our

brand.

Here it is validated and confirmed for

order.

Next is the model, which is an ATX

High Tower. Want to fit a lot in

there.

Page 33: Introduction To Programming IP5

PROGRAMMING 33

Again we have confirmation.

The color choice is black for the case.

Confirmation is again completed.

The size only comes in three sizes

MATX, ATX, or BTX so we are not

adding a number here. Just “N/A”.

It is confirmed in this splash. If

nothing were entered it WOULD

return and error.

Quantity of cases is added here. This

is where an integer is considered.

Page 34: Introduction To Programming IP5

PROGRAMMING 34

Validity is confirmed here.

Again, from before, we have input the

phone number request.

It is confirmed when entered

correctly.

Here the customer receives the

accomplishment page for their order.

It exhibits all information from before

and also addresses the extra classes

added in for this feature.

Page 35: Introduction To Programming IP5

PROGRAMMING 35

It also addresses the information in

the running virtual environment to

ensure everything is going to the

(file.write).

Here we see the files have been built

into the database.

When notepad is opened, all the

information presents itself to the

business to be filled.

Conclusion

The overall function of personal classes can greatly enhance the functionality of any

program being built. It’s a nice loop hole built in to create easy flowing programs that might

not already be built within the system. In a way it is like adding a few slang words to the

language vocabulary to enhance your overall communication of thought.

Page 36: Introduction To Programming IP5

PROGRAMMING 36

Chapter 5 - Inheritance and Polymorphism

Arrays and Sub Classes

Last week we integrated a superclass to apply objects to our programming fields. It

introduced the “get method” to pull information from certain objects, when needed. This week’s

work has expounded on those methods as to where subclasses have been integrated into the

program and through the use of inheritance and polymorphism, these files can be called on to

perform functions within the main program. These are used to generate instances within the

program when using arrays to perform the actions. When applying all these new subclasses, I

found there were a lot of modifications needed to the main program in order for everything to

operate in a functional means. The “main static void” had to be tremendously altered to

accommodate the new functions using inheritance and polymorphism techniques. Below is a

snap shot of the added information using inheritance techniques and polymorphism techniques.

openingMsg = "Welcome to Claw Concepts"+"\n" + "Shop for the computer of your dreams!"; JOptionPane.showMessageDialog(null, openingMsg);

prompt = "What's Your Name Please?"; customerName = getStringInput(prompt);

phn=VLDNO();

while (makeOrder.equalsIgnoreCase("yes")) {

prompt = "What section would you like today? " + "\n" + "Please ENTER Number Option : " + "\n" + " ( 1 ) for Desktop Systems " + "\n" + " ( 2 ) for Laptop Systems " + "\n" + " ( 3 ) for Tablets " + "\n";

Page 37: Introduction To Programming IP5

PROGRAMMING 37

section = getStringInput(prompt); secType = Integer.parseInt(section); if (secType == 1){ JOptionPane.showMessageDialog(null,"Section selected is: Desktops ");} else if (secType == 2){ JOptionPane.showMessageDialog(null,"Brand selected is: Laptops ");} else if (secType == 3){ JOptionPane.showMessageDialog(null,"Brand Selected is: Tablets "); }

prompt = "What brand will you be ordering today? : " + "\n" + " Please Enter : " + "\n" + " ( 1 ) for Hewlett Packard " + "\n\n" + " ( 2 ) for Dell - Alienware " + "\n\n" + " ( 3 ) for Gateway - Acer " + "\n\n"; brand = getStringInput(prompt); bType = Integer.parseInt(brand); if (bType == 1){ JOptionPane.showMessageDialog(null,"Brand selected is: HewlettPackard ");} else if (bType == 2){ JOptionPane.showMessageDialog(null,"Brand selected is: Dell ");} else if (bType == 3){ JOptionPane.showMessageDialog(null,"Brand Selected is: Acer Ware "); }

prompt = "Which model would you like ? " +"\n" + " Please Enter : " + "\n" + " ( 1 ) for HP Envy 810-160 " + "\n\n" + " ( 2 ) for HP Pavilion 17-e117dx " + "\n\n" + " ( 3 ) for HP 10 Plus 10.1-Inch 16 GB " + "\n\n" + " ( 4 ) for Dell OptiPlex Core i7 i7-3770 3.40 GHz mATX " + "\n\n" + " ( 5 ) for Dell Inspiron 17R Touchscreen Gaming Laptop " + "\n\n" + " ( 6 ) for Dell Venue 8 Pro 32 GB Tablet " + "\n\n"

Page 38: Introduction To Programming IP5

PROGRAMMING 38

+ " ( 7 ) for Acer Predator AG3-605-UR2E Core i7-4770 " + "\n\n" + " ( 8 ) for Acer Aspire E5-771-378Y " + "\n\n" + " ( 9 ) for Acer Aspire Switch 10 SW5-011-18R3 " + "\n\n"; model = getStringInput(prompt); mType = Integer.parseInt(model); if (mType == 1){JOptionPane.showMessageDialog(null, "Your selected HP Model Desktop is: HP Envy 810-160 "); costPerItem = 329.00;} else if (mType == 2){ JOptionPane.showMessageDialog(null, "Your selected HP Model Laptop is: HP Pavilion 17-e117dx "); costPerItem = 469.00;} else if (mType == 3){JOptionPane.showMessageDialog(null, "Your selected HP Model Tablet is: HP 10 Plus 10.1-Inch 16 GB "); costPerItem = 229.00;} else if (mType == 4){ JOptionPane.showMessageDialog(null, "Your selected DellModel Desktop is: Dell OptiPlex Core i7 i7-3770 3.40 GHz mATX "); costPerItem = 529.00;} else if (mType == 5){JOptionPane.showMessageDialog(null, "Your selected Dell Model Laptop is: Dell Inspiron 17R Touchscreen Gaming Laptop "); costPerItem = 479.00;} else if (mType == 6){ JOptionPane.showMessageDialog(null, "Your selected Dell Model Tablet is: Dell Venue 8 Pro 32 GB Tablet "); costPerItem = 139.00;} else if (mType == 7){JOptionPane.showMessageDialog(null, "Your selected Acer Model Desktop is: Acer Predator AG3-605-UR2E Core i7-4770 "); costPerItem = 429.00;} else if (mType == 8){ JOptionPane.showMessageDialog(null, "Your selected Acer Model Laptop is: Acer Aspire E5-771-378Y "); costPerItem = 289.00;} else if (mType == 9){JOptionPane.showMessageDialog(null, "Your selected Acer Model Tablet is: Acer Aspire Switch 10 SW5-011-18R3 "); costPerItem = 168.00;}

Page 39: Introduction To Programming IP5

PROGRAMMING 39

prompt = "What color option would you like?" + "\n" + " Please Enter : " + "\n" + " ( 1 ) for Black " + "\n" + " ( 2 ) for Silver " + "\n" + " ( 3 ) for Gun Silver " + "\n" + " ( 4 ) for Blue " + "\n" + " ( 5 ) for Green " + "\n" + " ( 6 ) for Red " + "\n"; color = getStringInput(prompt); cType = Integer.parseInt(color); if (cType == 1){JOptionPane.showMessageDialog(null, "Your selected Color is: Black ");} else if (cType == 2){ JOptionPane.showMessageDialog(null, "Your selected Color is: Silver ");} else if (cType == 3){ JOptionPane.showMessageDialog(null, "Your selected Color is: Gun Silver ");} else if (cType == 4){ JOptionPane.showMessageDialog(null, "Your selected Color is: Blue ");} else if (cType == 5){ JOptionPane.showMessageDialog(null, "Your selected Color is: Green ");} else if (cType == 6){ JOptionPane.showMessageDialog(null, "Your selected Color is: Red ");}

prompt = "Please enter Size? " + "\n" + " ( 1 ) for ATX " + "\n\n" + " ( 2 ) for BTX " + "\n\n" + " ( 3 ) for 17.3 inch Laptop " + "\n\n" + " ( 4 ) for 15.3 inch Laptop " + "\n\n" + " ( 5 ) for 10.1 inch Tablet " + "\n\n" + " ( 6 ) for 9.7 inch Tablet " + "\n\n" + "If size not needed. ENTER: N/A "; size = getStringInput(prompt); sType = Integer.parseInt(size); if (sType == 1){JOptionPane.showMessageDialog(null, "Your selected Desktop Size is: ATX ");} else if (sType == 2){ JOptionPane.showMessageDialog(null, "Your selected Desktop Size is: BTX ");} else if (sType == 3){ JOptionPane.showMessageDialog(null,

Page 40: Introduction To Programming IP5

PROGRAMMING 40

"Your selected Laptop Size is: 17.3 inches ");} else if (sType == 4){ JOptionPane.showMessageDialog(null, "Your selected Laptop Size is: 15.3 inches ");} else if (sType == 5){ JOptionPane.showMessageDialog(null, "Your selected Tablet Size is: 10.1 inches ");} else if (sType == 6){ JOptionPane.showMessageDialog(null, "Your selected Tablet Size is: 9.7 inches ");} else if (sType == 7){ JOptionPane.showMessageDialog(null, " Size Not needed Enter: N/A ");}

Using this method, we are capable of pulling up different options verses utilizing only single

question and answer methods and it makes the process more user friendly by adding the

ability to just enter numerical values; versus having to type in all the information. However,

by completing this style, arrays do have to be added to the process so that the program can

incorporate the number sequence for the user. Thus, the script presented, to work with the

aforementioned script above will look something like this and should return multiple values

with the virtual environment when initiated. The following is the required additional script as

well as a presentation of all possible values generated when the string has been started.

String[]Section; Section = new String[3]; String[]Brand; Brand = new String[3]; String[]Model; Model = new String[9]; String[]Color; Color = new String[6]; String[]Size; Size = new String[7]; Section[0]= "Desktops"; Section[1]= "Laptops"; Section[2]= "Tablets";

Page 41: Introduction To Programming IP5

PROGRAMMING 41

Brand[0]= "Hewlett Packard"; Brand[1]= "Dell"; Brand[2]= "Acer Ware"; Model[0]="HP Envy 810-160"; Model[1]="HP Pavilion 17-e117dx"; Model[2]="HP 10 Plus 10.1-Inch 16 GB"; Model[3]="Dell OptiPlex Core i7 i7-3770 3.40 GHz"; Model[4]="Dell Inspiron 17R Touchscreen Gaming Laptop"; Model[5]="Dell Venue 8 Pro 32 GB Tablet"; Model[6]="Acer Predator AG3-605-UR2E Core i7-4770"; Model[7]="Acer Aspire E5-771-378Y"; Model[8]="Acer Aspire Switch 10 SW5-011-18R3"; Color[0]="Black"; Color[1]="Silver"; Color[2]="Gun Silver"; Color[3]="Blue"; Color[4]="Green"; Color[5]="Red"; Size[0]="ATX"; Size[1]="BTX"; Size[2]="17.3 inch"; Size[3]="15.3 inch"; Size[4]="10.1 inch"; Size[5]="9.7 inch"; for (int i = 0;i <= 2;i++){ for (int j = 0;j <= 2;j++){ for (int k = 0;k <= 8;k++){ for (int m = 0;m <= 5;m++){ for (int n = 0;n <= 5;n++){ System.out.println("Section: "+Section[i]+" "+ "Brand: " +Brand[j]+" "+"Model: "+Model[k]+" "+ "color: "+Color[m]+" "+"size: "+Size[n]); } } }

Page 42: Introduction To Programming IP5

PROGRAMMING 42

} } Section sectionTypes = new Section(); Brand brandTypes = new Brand(); Model modelTypes = new Model(); Color colorTypes = new Color(); Size sizeTypes = new Size();

After I complete the writing portion of the script noted above, I was amazed by the immensity

of the number of lines required to get a string to run with all variables. Once completed the

output on the virtual environment within the program pumped out a whopping amount of

information and possibilities as is shown below. This generated over 300 pages of

information in the output window, but for the purposes of this document, I will only be

inserting three pages to represent some of the possibilities the program presented to evaluate

customer selection. Three hundred pages was a little too much for the doc.

Virtual Output

run:

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Black

size: ATX

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Black

size: BTX

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Black

size: 17.3 inch

Page 43: Introduction To Programming IP5

PROGRAMMING 43

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Black

size: 15.3 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Black

size: 10.1 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Black

size: 9.7 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Silver

size: ATX

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Silver

size: BTX

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Silver

size: 17.3 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Silver

size: 15.3 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Silver

size: 10.1 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Silver

size: 9.7 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Gun

Silver size: ATX

Page 44: Introduction To Programming IP5

PROGRAMMING 44

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Gun

Silver size: BTX

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Gun

Silver size: 17.3 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Gun

Silver size: 15.3 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Gun

Silver size: 10.1 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Gun

Silver size: 9.7 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Blue

size: ATX

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Blue

size: BTX

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Blue

size: 17.3 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Blue

size: 15.3 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Blue

size: 10.1 inch

Page 45: Introduction To Programming IP5

PROGRAMMING 45

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Blue

size: 9.7 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Green

size: ATX

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Green

size: BTX

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Green

size: 17.3 inch

Section: Desktops Brand: Hewlett Packard Model: HP Envy 810-160 color: Green

size: 15.3 inch

BUILD SUCCESSFUL (total time: 9 seconds)

Screen Shots of Operation with Arrays

As the program began to run, the following screen shots appeared.

The formal welcome screen is presented to the user.

The customer’s name is asked.

Page 46: Introduction To Programming IP5

PROGRAMMING 46

The use of an appropriate number is asked to be entered. The exception generator is still in place in case they make a mistake.

Once phone number is validated a valid response is issued.

The customer is now asked to enter a number option for which section they want to go to. Here, I have entered (2) for laptops

The program now confirms the laptop section has been selected.

The program now has for the brand that the customer desires to look at. Again, I have picked number 2 for Dell-Alien ware.

Page 47: Introduction To Programming IP5

PROGRAMMING 47

The program now confirms the laptop brand has been selected.

The system now asks which model I would like out of the Dells listed. I picked the Dell 17R touchscreen laptop.

It is confirmed as the Dell 17R.

Blue was my choice for a nice cover for the laptop.

Page 48: Introduction To Programming IP5

PROGRAMMING 48

The program verifies that it is blue that has been selected.

In this frame, the customer chooses the size of their system. The first two are for desktops, the next two are for laptops and the last two are for tablets. I am choosing (3) for the 17 inch screen.

Here, we see that option 3 is chosen and verified for the 17 inch screen.

In this frame the customer is asked for the quantity. I picked three laptops as I am greedy and like laptops.

Quantity is verified for three laptops. Hope I have enough on my credit card.

Page 49: Introduction To Programming IP5

PROGRAMMING 49

The system asks me if I want to order anything else. I think I spent enough.

Here, the final message shows the numbers picked for my choices. It also shows cost per item, tax, and total cost of purchase. Now I get to go out and work so I can pay it all off.

Finally, we have the document that was sent to file in the cc-final program.

The itemized order needed to fulfill the order by the customer.

Page 50: Introduction To Programming IP5

PROGRAMMING 50

Conclusion

In this phase, it was shown how to put polymorphism and inheritance into the

program to supercharge the overall capability of the program. By building a superclass,

adding methods to the superclass and inheriting additional files to pull from; we were

able to make the program more user friendly when entering the required information to

fulfill the order. The customer was now able to enter just numbers instead of having to

type or enter the required information into the system. Polymorphism and inheritance

give the program a stream lining affect for the computer, customer and programmer. It’s

a win-win situation for all parties involved.

References

There are no sources in the current document.

All entries developed by the author.