editing, compiling, executing and submitting programs · [abcd1234@csc abcd1234]$ another useful...

10
Editing, Compiling, Executing and Submitting Programs Quick Start Compile step once always mkdir labs javac Realtor.java cd labs Execute step mkdir 0 java Realtor cd 0 Submit step emacs Realtor.java & submit csc156abc 0 Calculating Based Upon Home Sales For most employees who happen to work in sales, salary is often based upon calculations that are determined by a percentage of their sales. This is sometimes the case for calculating taxes as well. For this assignment, the transaction involved is the sale of a house. There is the cost associated with the sale that is arbitrarily determined to be 6% of the sales price of the house, while the sales person’s commission is just as arbitrarily determined to be 1.5% of the sales price of the house. Each percentage is, of course, representable as a fraction with 100 as the denominator. Consequently, 6% is 0 to 6 100 while 1.5% is equivalent to 1.5 100 = 15 1000 . Consequently, if we are interested in calculating the percentage of a value, we simply multiply the equivalent fraction of the percentage by the value. For example, let’s assume that the sales price of our house is $100,000. To determine the cost and 0 associated with 3 the house, we want to know the following. cost = 6% of $100,000 = 6 100 · $100, 000 = 0.06 × $100, 000 = $6, 000 commission = 30% of $100,000 = 1.5 100 · $100, 000 = 0.015 × $100, 000 = $1, 500 Operating systems and Graphical User Interfaces Since the days of the first electro-magnetic computers, operating systems have provided software that allows users to control a variety of tasks that are necessary to effectively use the machine. Think of the operating system commands as software that is already written to control the machine. In other words, you don’t have to write it. There are actually several operating systems that constantly need to interact within Oakton’s Local Area Network (LAN). The network servers usually run a network operating system such as Novell’s. These are necessary for a variety of telecommunications activities such as loading software on your machine from a remote location or printing a file on one of the Oakton LAN printers. When you begin your computer session in any of the Oakton labs, your identity is first verified by a Novell server and then the machine in front of you boots the Microsoft Windows operating system. Most of the applications that are used in the Oakton laboratories run under the Microsoft Windows operating system. When the time comes for you to actually program, you will 1

Upload: others

Post on 05-Aug-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Editing, Compiling, Executing and Submitting Programs · [abcd1234@csc abcd1234]$ Another useful command is the Manual (man) command. This command retrieves the manual page for any

Editing, Compiling, Executing andSubmitting Programs

Quick Start Compile step

once always

mkdir labs javac Realtor.java

cd labs Execute step

mkdir 0 java Realtor

cd 0 Submit step

emacs Realtor.java & submit csc156abc 0

Calculating Based Upon Home Sales

For most employees who happen to work in sales, salary is often based upon calculations that are determinedby a percentage of their sales. This is sometimes the case for calculating taxes as well. For this assignment,the transaction involved is the sale of a house. There is the cost associated with the sale that is arbitrarilydetermined to be 6% of the sales price of the house, while the sales person’s commission is just as arbitrarilydetermined to be 1.5% of the sales price of the house.

Each percentage is, of course, representable as a fraction with 100 as the denominator. Consequently, 6% is 0 to6

100 while 1.5% is equivalent to 1.5100 = 15

1000 . Consequently, if we are interested in calculating the percentage of avalue, we simply multiply the equivalent fraction of the percentage by the value. For example, let’s assume thatthe sales price of our house is $100,000. To determine the cost and 0 associated with 3 the house, we want toknow the following.

cost = 6% of $100,000 = 6100 · $100, 000 = 0.06× $100, 000 = $6, 000

commission = 30% of $100,000 = 1.5100 · $100, 000 = 0.015× $100, 000 = $1, 500

Operating systems and Graphical User Interfaces

Since the days of the first electro-magnetic computers, operating systems have provided software that allowsusers to control a variety of tasks that are necessary to effectively use the machine. Think of the operatingsystem commands as software that is already written to control the machine. In other words, you don’t have towrite it. There are actually several operating systems that constantly need to interact within Oakton’s LocalArea Network (LAN). The network servers usually run a network operating system such as Novell’s. These arenecessary for a variety of telecommunications activities such as loading software on your machine from a remotelocation or printing a file on one of the Oakton LAN printers. When you begin your computer session in anyof the Oakton labs, your identity is first verified by a Novell server and then the machine in front of you bootsthe Microsoft Windows operating system. Most of the applications that are used in the Oakton laboratories rununder the Microsoft Windows operating system. When the time comes for you to actually program, you will

1

Page 2: Editing, Compiling, Executing and Submitting Programs · [abcd1234@csc abcd1234]$ Another useful command is the Manual (man) command. This command retrieves the manual page for any

CSC 156 - Assignment 0 Realtor

telecommunicate with another computer, unseen by you, that is running the LINUX operating system. Thismeans of working on a computer that you are not physically sitting in front of is the state of the art in networkedcomputing.

Most of your experience with operating systems may actually have been filtered through a graphical user interface(GUI) such as those found at your ATM, on your cellular phone or a GPS mapping system. This is a set ofsoftware applications that allow you to use a pointer device such as a mouse to communicate your commandsby clicking on icons and menu selections rather than having to type them in at the keyboard. How tightly anoperating system is integrated with its GUI can vary from one operating system to another. When the MicrosoftWindows interface first came out, it was loosely installed onto the MS-DOS operating system that was runningon the machine making it possible to run applications through either the Microsoft Windows GUI or from theMS-DOS prompt. With the introduction of Microsoft Windows 95, the GUI became far more integrated to theunderlying operating system. The MS-DOS prompt is still accessible, but it is becoming more and more difficultto find any software that can be successfully run from that environment. Users of early Apple products such asthe Apple IIe also had access to a command prompt from which they could issue commands. The Macintoshwas the first commercially successful desktop machine that possessed a Windowing environment that removedthe need of the command prompt. Like all operating systems, UNIX has a GUI available to it as well, calledX. This will provide you with functionality similar to the MacOS or the Microsoft windowing environments, butwill also have access to a command prompt.

Logging into your UNIX account

The name of Oakton’s LINUX machine is csc.oakton.edu. LINUX acts very similar to UNIX, but does nothave the licensing fees associated with it that UNIX has. This is how the developers of LINUX have been ableto give their product away over the Internet. If you are logged into one of the Oakton computers under theMicrosoft Windows environment, you will need to begin your telecommunications session to csc.oakton.edu

through software called ssh. This will allow you to telecommunicate in a non-graphical manner with a remotecomputer. Use the mouse to click on the Start icon, then find the text field entitled Search programs and fileson the bottom of your screen. Type Putty in that window to search for that application and launch the softwareby clicking on the entry that appears with the ssh icon shown in Figure 1. Modify the Host Name, Port and

Figure 1: ssh Icon for Invoking Putty

Connection type options in the configuration menu to reflect the values shown in Figure 2. Then, click on theOpen button.

The first time that you run ssh, you will be prompted to confirm the authenticity of the host (in this case,csc.oakton.edu). Reply Yes to the prompt regarding your interest in continuing to connect. You will then beprompted for your LINUX userid and your LINUX password. The userid is the same as your Oakton networkuserid. Your initial password for this account is your Oakton Banner id which is a capital B or capitalA followed by 8 digits. Passwords are not displayed on the monitor, so watch the keyboard as you enter thevalues in. Once you are logged into the LINUX machine, we want to set up the parameters that are necessaryfor us to telecommunicate in a graphical environment. Begin by executing the following command to initiate theVNC server software.

vncserver

You will be prompted for your VNC password which we recommend should be your regular Oakton network

2

Page 3: Editing, Compiling, Executing and Submitting Programs · [abcd1234@csc abcd1234]$ Another useful command is the Manual (man) command. This command retrieves the manual page for any

CSC 156 - Assignment 0 Realtor

Figure 2: Putty Configuration for csc.oakton.edu

password (not the Banner id number discussed above). You will need to verify this password by typing it intwice, but you will only need to do this step the first time that you run vncserver. The command will respondwith a statement that indicates the VNC port number. For example, the following response indicates that yourVNC session can use port 16. Remember your port number for this session, it may be different every time thatyou login.

New desktop is csc:16

If you’ve never installed the RealVNC Viewer application on your machine, you’ll need to downloadit and install it from the following URL. http://www.realvnc.com/en/connect/download/viewerRun the application once it is installed.

When prompted for the VNC Server type the following address into theframe text box where VNC port number was the value returned by the

vncserver command. csc.oakton.edu:VNC port number For instance,

in this example you would enter the server as csc.oakton.edu:16 Youmay need to relax the firewall settings on your virus protection software tocomplete the next step.

You will then be prompted for your VNC password. Once again, use thesame password that you used in response to the vncserver command above.

When your desktop opens, find the Applications menu at the top of your frame and then find the System Tools

sub-menu. Within System Tools , find the Terminal application and click on that. A window called an X-

terminal, or xterm will open up and display a command prompt that is waiting for you to enter in commands.If your userid is abcd1234, the command prompt might look something like the following.

3

Page 4: Editing, Compiling, Executing and Submitting Programs · [abcd1234@csc abcd1234]$ Another useful command is the Manual (man) command. This command retrieves the manual page for any

CSC 156 - Assignment 0 Realtor

[abcd1234@csc abcd1234]$

In this discussion, anything that you should type into the computer will be typeset in a boldfaced monospacedfont such as this font. The response that the computer makes will be an italicized version of the same fontsuch as this font. When we wish to discuss commands that we’re not immediately interested in your typing in,we will use a normal monospaced font, such as this font. There are a variety of UNIX commands that youcan type in at the command prompt, some of which are described in the Oakton UNIX Quick Reference Guide.We’ll demonstrate a few of them below.

Some essential commands

The final portion of the command prompt that is displayed in your xterm is not only your userid, but it isalso your home directory. File systems are partitioned into directories to help organize them. When you loginto your LINUX account, you will be automatically placed into your home directory. Your home directory is asub-directory of another directory, which in turn can be a sub-directory of yet another directory. This continuesup until the root of the directory system, which unlike other operating systems, is designated with a slash /,rather than a letter of the alphabet, c:.

To discover the path to your current the directory, you can use the Print Working Directory (pwd) command.Type this in at your xterm prompt and strike the return key.

pwd

The response should look something similar to the following. Don’t concern yourself if yours is slightly different.Just remember that you can always use this command to find where you are in your directory structure. Noticehow the prompt repeats so that you are able to enter another command in.

/home/csc/stud/abcd1234

[abcd1234@csc abcd1234]$

Another useful command is the Manual (man) command. This command retrieves the manual page for anycommand that the operating system supports. Try to pull up the manual page for the pwd command by typingthe following in at the xterm command prompt.

man pwd

The response will probably take more than one page of display. If it does, you should see a semi-colon (:) at thebottom of the page. This simply means that there is more information pending. If you wish to read the nextpage, simply strike the space bar and the next page will appear. If you do not wish to read subsequent pages, usethe keystroke q to indicate that you wish to quit. When you have reached the end of the manual page, LINUXwill display the (END) message. Again, the keystroke q should be used to indicate that you wish to quit.

In order to start our program, we’d like to create some sub-directories to help us keep things organized. Sinceour programming assignments are referred to as laboratories, why don’t we create a sub-directory of our homedirectory called labs. First of all, let’s ensure that we are in our home directory. You can issue either the pwd

command and make sure that it returns the same value that we saw above, or use the Change Directory (cd)command. When you issue the cd command without a target directory, you will always return to your homedirectory. Try it.

cd

Now, let’s create the labs directory with the Make Directory (mkdir) command.

4

Page 5: Editing, Compiling, Executing and Submitting Programs · [abcd1234@csc abcd1234]$ Another useful command is the Manual (man) command. This command retrieves the manual page for any

CSC 156 - Assignment 0 Realtor

mkdir labs

Notice how with each command, LINUX simply returns to the prompt. This will always be the case when thecommand succeeds. When something goes wrong, LINUX will issue some type of error message indicating whatthe problem was. To ensure that our directory has been created, let’s issue the List directory (ls) command.

ls

The labs directory should be part of the display resulting from this command. Now let’s descend into thatdirectory.

cd labs

If we now issue the pwd command, we should see the labs directory tagged onto the end of the path, such asthe following.

/home/csc/stud/abcd1234/labs

One more directory and we’ll be ready to start typing our program in. Since this exercise is called Assignment0, let’s create a 0 directory under the labs directory. The following two commands should do the trick.

mkdir 0

cd 0

Note that we’ve typed the number 0 for the above and not the capital letter O. Now the pwd command shouldrespond with the following.

/home/csc/stud/abcd1234/labs/0

Commands for Software Development

Editing with emacs

The emacs editor provides a powerful environment for editing, compiling and debugging your program. Youbegin your emacs session by issuing the emacs command and the file that you wish to edit. Type in the followingcommand at your xterm prompt.

emacs Realtor.java &

The reference to Realtor.java is the name of the file that you wish to edit. The & at the end of the statementdirects the operating system to execute the command in the background. What this means is that your LINUXcommand prompt will return in the xterm window after you issue the command. A second window will openand you will have two seemingly independent windows to work in, your xterm window and your emacs window.If you leave the & out of the above command, you will need to close down your emacs window in order to issueany additional commands.

When emacs opens on a file that is empty, the window display shows 4 areas of interest. Across the top of thescreen are a variety of menus that we can use to manipulate emacs. Across the bottom of the screen is an areacalled the mini-buffer which will display commands that we choose from the menus. The thin line directly abovethe mini-buffer provides a variety of information including the file that we are currently editing (Realtor.java),

5

Page 6: Editing, Compiling, Executing and Submitting Programs · [abcd1234@csc abcd1234]$ Another useful command is the Manual (man) command. This command retrieves the manual page for any

CSC 156 - Assignment 0 Realtor

the type of file that it is (Java) and the line number that our cursor is positioned on (L1). The largest area ofthe frame above the informational area is where our typed characters will display.

You will need to click on the emacs window to activate it. Type in the program at the end of this document payingattention to punctuation, spacing and style. You should type your name and today’s date in the appropriatepositions of the code. You can use the traditional keys for cursor movement (arrows, Page-Up, Page-Down) andinsertion or deletion of keystrokes (space bar, Backspace, Delete). Emacs will color code various aspects of theprogram including comments, keywords, variable names and string constants.

When you have finished typing, you will need to save your program to the file system of csc.oakton.edu. Thiscan be done by taking the Save command from under the Files menu of emacs. Failure to accomplish this stepcorrectly may result in you having to type your program in again.

Things to Think About

Many editors wrap lines so that it appears that there is a second line in the display even though it is only onelong line. Sometimes, you can configure the editor to not wrap so that the view will shift to the right and stayon the same line when you run out of room to type. Emacs has this ability under the Options menu. What’simportant in these cases is that you make sure that you strike the Enter or Return key when you reach theend of the line. That inserts a newline character that the compiler looks for and responds to. A classic exampleis the following. Let’s say that you have a long comment (a line that is ignored by the compiler) followed by avariable declaration (a line that is critical to successful compilation). So, we have the following:

// this is a long comment followed by the declaration of a variable named x

double x;

This needs to be compiled as two lines separated by the newline (Enter) keystroke. If this gets wrapped andis really stored as one line, the compiler can not see the declaration as separate.

Java code is case sensitive so String is different from string and System is different from system.

Some characters that appear alike on the keyboard are going to be stored differently on the computer.Classic examples include the small letter ell (l) as compared to the number one (1) as well as the capital letteroh (O) as compared to the number zero (0). Particular to the Java language is the confusion between the double-quote character (") as compared to 2 keystrokes of the apostrophe character (’). They may look similar, butare in fact stored differently in the computer and thus will lead to different (mostly erroneous) results.

Compiling within emacs

Once your program has been saved, you can begin the compilation process by choosing the Compile... commandfrom under the Tools menu of emacs. When you make this choice, emacs will respond with the following in themini-buffer.

Compile command: make -k

Click on the mini-buffer area to delete the make -k and change this command to the following.

Compile command: javac Realtor.java

This invokes the javac compiler to compile the program in the file Realtor.java to create an executable calledRealtor.class. At this point, the screen will split into 2 frames. The top one will be a continuation of youredit session on Realtor.java. The bottom will be the results of your attempt to compile.

There are two types of errors that can occur in you programs. Syntax or compiler errors represent errors inthe grammatical use of the language. This could be caused by bad punctuation, bad spelling or mismatched

6

Page 7: Editing, Compiling, Executing and Submitting Programs · [abcd1234@csc abcd1234]$ Another useful command is the Manual (man) command. This command retrieves the manual page for any

CSC 156 - Assignment 0 Realtor

parentheses in one of your expressions. If you have errors in your compilation, they are these types of errors.The other type of error is a logic or execution time error. This type of error occurs only after your executablehas been created.

If your program has syntax errors, you will need to correct them before you proceed. The bottom frame of emacswill display the line number that the compiler believes is responsible for the error. You can travel to that lineby using the arrow or Page-Up or Page-Down keys and watching the line designator on the informational lineabove the mini-buffer. Another manner to travel to a specific line is to use the Menu system to execute the Edit

-> Go To -> Goto Line commands. The fastest manner to parse compiler errors is to issue the keystroke C-x

‘ (Depress the Ctrl key while striking the x key. Then release the Ctrl and x keys and strike the ‘ key).

An editor can only guess at the line that caused the error. Carefully compare the line in question with the lineof the source code. If they appear identical, look at the line above that line as well. If there are commentsinvolved, look at the first executable line above the comment. Often an error on an earlier line causes a later lineto be tagged by the compiler as an error. When you have found your errors and repaired them, you will need torecompile.

After choosing the Compile... command from the Tools menu, emacs will prompt you if you want to save thechanges that you’ve made. It will do so when you respond to the prompt with the letter y. After you’ve savedyour repairs to the file system, recompilation will occur. When your program compiles correctly, you will see themessage Compilation finished at ... and you are ready to test the execution of your program.

Executing and testing your program

Clicking on your xterm window will activate it so that you can enter commands at your command prompt.Execute your program with the following command.

java Realtor

Your program is controlling the computer and messages that you see being displayed are those that your programdirected to be displayed. At this point, you can determine if there are any logic errors in your program. Youshould enter test data for input that you can predict the output values to and verify that the output is beingcorrectly displayed. If you can not get your program to produce the predictable output, you will have to goback to your emacs session and discover any lines that you omitted or typed incorrectly into your program.Finally, you should enter some unpredictable values for input into your program to see if you can discover itslimitations. All programs have limitations, we just have to spend some time searching for them. Your knowledgeof mathematics can help you here. You can also use tools such as Mathematica or your calculator to help youdevelop test data. When you are satisfied that your program is executing correctly, you can close down youremacs session by choosing the Exit Emacs command from the Files menu.

Maintenance and Beyond

Formatting of Real Data

The discussion of formatting of numeric data represents a programmer’s interest in taking complete control overhow the data appears on the screen. In the program, we’re simply printing String values, some real numbers andtheir identification. The identification consists of String values that identify either the seller, price, cost or0 for the sale. For this String data, we’re simply interested in printing whatever value has been stored or coded.For real numbers that represent the output of our double variables, the choices are a bit more complicated. Firstof all, we have the choice of justification. Usually numeric data is printed right-justified so that when representedin a table, the decimal points are likely to line up. In addition, we have the option of printing real data in eithera fixed format where the output consists of numbers that are surrounding a decimal point or a scientific formatwhere the output consists of a real number between 1 and 10 multiplied by a power of 10. In general, if youhave a sense of how large the numbers are going to get as a result of your program’s execution, the fixed formatis advisable since you can predict how many digits it will take to display your results. Otherwise, you might be

7

Page 8: Editing, Compiling, Executing and Submitting Programs · [abcd1234@csc abcd1234]$ Another useful command is the Manual (man) command. This command retrieves the manual page for any

CSC 156 - Assignment 0 Realtor

well advised to use the scientific format until you’re sure as to the magnitude of the output of your program’sexecution.

In Java, we can proceed to format this output in the following manner. To begin with, we’ll need to replace eachof our System.out.println() statements with a System.out.printf() statement. This is where we will listthe manipulators that we wish to move forward with. For String values that identify our numbers, we merelymake sure that what we’d like printed is delimited with the double quote "" characters in the output statement.For the numeric data, let’s arbitrarily agree that we’d like to display the data with 2 digits to the right of thedecimal point in fixed notation. To generate this format on a real number, replace the concatenation of theString identification and the real expression with the String value and the format descriptor %.2f embeddedin the String. After the String values, you place the real expressions to be printed separated by commas. Inthis environment, the line skip is generated with the %n descriptor. So, instead of using the following outputstatements.

// 4) display the results

System.out.println("\nThe " + seller + "’s home sold for " + price);

System.out.println("The cost to sell the home was " + cost);

System.out.println("The selling or listing agent earned " + commission);

To format the values, we would use the following output statements.

// 4) display the results

System.out.printf("%nThe " + seller + "’s home sold for $%.2f%n", price);

System.out.printf("The cost to sell the home was $%.2f%n", cost);

System.out.printf("The selling or listing agent earned $%.2f%n", commission);

There is an example of correctly formatted sample output for this program at this link .

Printing your program

You can print your program on any of the Oakton networked printers by first identifying the number of the labthat you are located in. This number is usually displayed on a sticker along with a machine number that appearseither on the monitor or the base of your computer. For instance if your computer contains a sticker with thenumber 1234-56 on it, then you are in room 1234 and using computer 56. If this is the case, print a copy ofyour program with the following command.

printer 1234 Realtor.java

All that needs to be identified to the printer command is the room number (1234) and the file name (Realtor.java).Retrieve your copy from the printer before proceeding.

Electronic submission of your program

In order to electronically submit your program to the database that allows your instructor to retrieve it, youwill need to be able to identify your course, your section number and the assignment number thatyou are submitting. For this discussion, lets assume that you are enrolled in CSC 156 abc and are interested insubmitting assignment number 0. The following command can be used to turn your program in.

submit csc156abc 0

All that needs to be identified to the submit command is the course and section number (csc1560c1) and theassignment number (0). The command should respond with a Successfully submitted message. Please emailyour instructor whenever you have trouble successfully submitting an assignment.

8

Page 9: Editing, Compiling, Executing and Submitting Programs · [abcd1234@csc abcd1234]$ Another useful command is the Manual (man) command. This command retrieves the manual page for any

CSC 156 - Assignment 0 Realtor

Logging out of your UNIX session

The command exit will end your xterm

session. Find the Disconnect icon(third from the left) button on the topof your frame and click on that.

You will then need to close the X back-ground window by clicking on the boxedx icon in the upper right-hand corner.

You should then close your ssh sessiondown with another exit command typedin at the prompt. You should return tothe OCCnet Windows environment.

Now you only need to shut down yourWindows session. This can be done byclicking on the Start icon, followed by theShut Down command and then followedby choosing the Shut Down button in theresulting window. When you are informedthat it is safe to do so, you should powerdown both the monitor and the base unit.

9

Page 10: Editing, Compiling, Executing and Submitting Programs · [abcd1234@csc abcd1234]$ Another useful command is the Manual (man) command. This command retrieves the manual page for any

CSC 156 - Assignment 0 Realtor

/*************************************************************************

* Name: Your name CSC 156

* Date: Today’s date Lab 0

*************************************************************************

* Statement: Determine owner, selling cost and commission for house sale

* Specifications:

* Input - owner (String) and selling price (double)

* Output- owner (String), selling cost (double) and commission (double)

*************************************************************************/

// java class for keyboard I/O

import java.util.Scanner;

public class Realtor

{public static void main(String[] args)

{// declaration of objects

Scanner console = new Scanner(System.in); // for keyboard input

String seller; // seller’s name

// price of the house, the cost to sell the house and the commission

double price, cost, commission;

// 1. Output descriptive messages

System.out.println("This program calculates the cost to sell a home\n"+ "and the commission paid to an individual sales agent.\n");

System.out.println("The user is asked for the last name of the seller and the\n"+ "sales price.\n");

// 2. Prompt for and input the seller’s name

System.out.print("Please enter the owner’s last name: ");

seller = console.nextLine();

// 3. Prompt for and input the sales price

System.out.print("\nPlease enter the sales price of the home: ");

price = console.nextDouble();

// 4.-5. calculate the cost and the commission

cost = 0.06 * price;

commission = 0.015 * price;

// 6.-7. display the input and results

System.out.println("\nThe " + seller + "’s home sold for $" + price);

System.out.println("The cost to sell the home was $" + cost);

System.out.println("The selling or listing agent earned $ + commission);

}}

10