CSE 110 – Lab 1 What this Lab Is About: Setting up and using an IDE to run a basic Java Program.
CSE 110 – Lab 1 What this Lab Is About: Setting up and using an IDE to run a basic Java Program. Problems Solving, Arithmetic Expressions, Input/Output Getting input from user Step 0: Setup on a Windows PC using TextPad TextPad is a basic IDE that has all we need for this class. In order to use TextPad we first must download and install the Java Development Kit (JDK) which contains the tools needed to compile and execute Java programs. Be sure to download and install the JDK before TextPad. Accept the licensing terms and download and install the appropriate JDK for your PC from the following link: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html After downloading the JDK, then you can download and install TextPad from http://www.textpad.com/download/index.html After installing TextPad open the program and check that it successfully linked to the JDK you downloaded. Do this by choosing Tools from the toolbar and scrolling down to External Tools at the bottom. You should see three Java options when it is expanded (Compile Java, Run Java Application, Run Java Applet). If these do not show up ask your TA for help. To use TextPad to create a program first save the file with the same name as the class name as a .java file. Java is Case Sensitive. Then as you type the program below into TextPad, it will color code your text and indent for you. When you have finished typing your program (or part of your program), then you first need to compile your program. This is done by pressing ctrl+1 (ctrl and 1 together) or choosing Tools from the toolbar, then External Tools and Compile Java. Then to run the compiled program press ctrl+2 or choose Tools -> External Tools -> Run Java Application. Setup using Eclipse for a Mac or PC TextPad only runs on Windows, so we cannot use it for Macs. Instead we will use Eclipse. Eclipse is a larger IDE than TextPad with many more features, some of which will be helpful. Windows users may also prefer to use Eclipse for some of the extra features. To run Eclipse, you need to have JRE installed on your computer. Download and install the latest JRE or JDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html Then download and install the Eclipse IDE for Java Developers for your computer environment at the following link: http://www.eclipse.org/downloads/ After Eclipse is installed start it up. It will ask you for a workspace to save your files. Change this to whatever you want and pay attention to this as this is where you will find the files on your computer that you have to submit. After Eclipse opens you will have to create a new Java project in order to create any files. To do this choose File -> New -> Project. Give the project a name and click on Finish below. Next you have to add a Class to your Project. To do this right click on the project in the package explorer on the left of the screen and then select New -> Class. Name the class the appropriate name, for example you should call the class you create here Lab1 and click on Finish. The editor will then open and you can type in whatever you need to finish the program. To compile and run the program click on the green play button in the toolbar. The output will show up in a console at the bottom of Eclipse. Problem Description: Finding an Average Your friend Jenny has a class that gives three tests. She would like you to write a program that will take the three test grades as input and tell her what her average test grade is. For this Lab you are required to write a program that will read three test grades from the user and then calculate and print the average of those three test grades. Step 1: Getting Started Create a class called Lab1. Be sure to name your file Lab1.java. Assignments Documentation: At the beginning of each programming assignment you must have a comment block with the following information: /*————————————————————————- // AUTHOR: your name // FILENAME: title of the source file // SPECIFICATION: description of the program // FOR: CSE 110- Lab #1 // TIME SPENT: how long it took you to complete the assignment //———————————————————–*/ Step 2: Setting up a Scanner for Input Since you are required to read in the three test grades from the user, you will have to use a Scanner. Follow the instructions in Chapter 2 or in the book on page 49 to import the Scanner class from the java.util library and create a Scanner object to get input from the keyboard (System.in). Step 3: Declaring Variables Examining the problem, we see that we will need three inputs from the user. We will need variables to hold all of the inputs. For this Lab, let’s assume that all the test grades will be integers. Therefore, we will need three int variables to hold the three test grades. Remember, if you need more than one variable of the same type, you can declare them in the same statement separated by commas. For example if we needed two double variables, we could declare them like: double var1, var2; Declare three int variables to hold the three test grades. Be sure to give them appropriate names like test1, test2, etc. rather than x, y, z. Additionally, looking at the problem, we see that we have the number 3 occurring in the problem. Rather than simply using this number in the program when needed, it is preferable to declare a constant variable to hold the number so that when it is used in the program, it will be clear what the 3 refers to. Remember to create a constant you use the keyword final in front of the declaration. Also it is customary to use ALL_CAPS for the name of the constant. For example if we wanted a constant to hold the value PI, we would declare final double PI = 3.14159; Declare an int constant to hold the value 3, the number of tests. Be sure to give the constant an appropriate name like NUM_TESTS. Finally, when looking at a problem you may need variables to hold the solution or some intermediary steps. For this problem we need to calculate an average. We will need a variable to hold the average. Usually, the average of values can contain decimal values, so you will need to declare a double variable to hold the average. Step 4: Getting the Input Now that we have the needed variables declared, we are ready to use the Scanner we created to get the input from the user. Before reading in the input though, it is important to give the user a prompt so the user knows what they are expected to enter. Then we use the Scanner object with the appropriate method to read in the value and store it in a variable. For example to prompt and read in the first test score, we would use System.out.print(“Enter the score on the first test: ”); // prompt test1 = in.nextInt(); // read in the next integer where the already declared variable test1 will hold the score for the first test and in is the Scanner object. Write the prompt and read the input for all three tests. Step 5: Calculate the average After reading the three input values from the user, we can use them to calculate the average. To do so we add up all the values and divide them by the number of tests. Naively, this would be average = test1 + test2 + test3 / NUM_TESTS; However, due to operator precedence rules Java will do the division, test3 / NUM_TESTS, before the addition, which will give the wrong result. To force Java to do the addition first, we have to use parentheses average = (test1 + test2 + test3) / NUM_TESTS; This will calculate the average, but there is still a problem. Assume the test grades are 90, 90, and 92, then the average will be 90.6666, but Java will give the answer as 90 (You should run the program and print the result to verify). This is because all the variables are integers and so Java does integer division. To force Java to do decimal division, we have to cast one of the variables to a double. Remember to cast a value to another type you put the type you want to cast to in parentheses before the value. So, let’s cast NUM_TESTS to a double average = (test1 + test2 + test3) / (double)NUM_TESTS; We could have cast any of the other variables as well. Calculate the average test score in your code. Step 6: Display Results Now that we have calculated the result we need to show it to the user. Use a System.out.println statement to display the average score to the user. Be sure to have a statement explaining what the number is. That is, don’t just print the number. For example, if we wanted to print the first test score, we would use the following statement: System.out.println(“Your first test score: ” + test1); Use the following Coding Guidelines (You will be graded on this): Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). Keep identifiers to a reasonably short length. Use upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects). Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent. Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Please Note See the sample output below in the lab for an idea of what your program should output. Sample Output Below is an example of what your output should roughly look like when this lab is completed. All text in bold represents user input. Sample Run 1: Enter the score on the first test: 90 Enter the score on the second test: 91 Enter the score on the third test: 92 Your average score is: 91.0 Sample Run 2: Enter the score on the first test: 90 Enter the score on the second test: 90 Enter the score on the third test: 92 Your average score is: 90.6666 Last Step: Submit your lab by following the instructions below: ********************************************************************************* Submit your Lab1.java file to the Submission Server. Go to the Submission Server site, https://courses.eas.asu.edu/cse110b/ login, then click on Lab Submissions in the left frame. The dropdown box will start in Lab1, so you don’t have to change it (you will for future labs). Click on the browse button and find where you saved your Lab1.java file (and not the Lab1.class file) on your computer. Upload the file to the site and then click on the Submit button. Your file will be submitted and a screen will show up displaying if your program compiled and what your output is when run on some sample input (in this case nothing). You should then check to make sure that the actual file submitted properly and is readable to the grader. To do so click on Grades in the frame on the left of the page and then click on the 0 underneath Lab1. You will again see that your program compiled and the sample output, but you should scroll down to the bottom of the screen and make sure your file is readable as well.
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"
