3. use indentation. Save the java file as Decimal MathUsing Integers.java in your lab4 folder. Compile...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
3. use indentation. Save the java file as Decimal MathUsing Integers.java in your lab4 folder. Compile the program and run it. Make sure that your program outputs what you see in Figure 2. If you have typed any part of the code incorrectly (and introduced a syntax error, you'll have to fix the error, until your code is error-free). //import scanner import java.util.Scanner; public class DecimalMathUsing Integers { //main routine public static void main (String[] args) { //declare variables and assign int wholePart=0, fractionalPart = 0; double composed DecimalValue = 0.0; Scanner keyboard = new Scanner(System.in); //instruct user System.out.print("Input an integer for digits left of the decimal: "); whole Part = keyboard.nextInt(); System.out.print("Input an integer> for digits right of the decimal: "); fractional Part keyboard.nextInt(); //output status message and calculations System.out.println("The whole Part variable has the value: + wholePart); System.out.println("The fractional Part variable has the value: " + fractionalPart); composedDecimalValue = whole Part + fractional Part/100.0; System.out.println("The composed DecimalValue is: " + composed DecimalValue); //mult composed Decimal by 10,000 and format with commas and 6 decimal places System.out.printf("The formatted composed DecimalValue times 10,000 is: %, .6f \n", (composed DecimalValue * 10000.0)); Figure 1: The program DecimalMathUsing Integers ----j GRASP exec: java Decimal MathUsing Integers Input an integer for the digits to the left of a decimal: 41 Input an integer >= 10, for the digits to the right of the decimal: 95 The leftOfDecimalDigits variable has the value: 41 The rightOfDecimalDigits variable has the value: 95 The composed DecimalVal is 41.95 The formatted composed Decimal Val times 10,000 is 419,500.000000 jGRASP: operation complete. Figure 2: Sample output of the code shown in Figure 1. Page 2 of 5 ว ZOOM + II. The switch statement By now, you've seen in lecture many examples of the switch statement. Review those slides if you need a refresher. This part of the lab will help you with the programming portion of homework #3, where you must use a switch statement. For the second part of this lab, you'll write a program, OneThroughNine java, that asks the user to input an integer 1 through 9, and then prints an interesting fact about the selected number. The "interesting" facts about each of the integers that your program must output is shown in the following table: Table 1: Interesting facts about the integers 1 through 9 Interesting fact Integer 1 1 can be written as 1*1, thus it has two factors, but they both are not different and hence it is not a prime number 2 2 is the only prime number that is even and it is the smallest prime number 3 3 squared is 9, and the square root of 9 is 3 4 4 is the smallest square greater than 1 5 5 is a half of 10. Wow! 6 6 is the smallest number that is equal to the sum of its proper divisors. Interesting! 7 The numbers 7 and 8 are both less than 9. Amazing! 8 The numbers 7 and 8 are both less than 9. Amazing! 9 3 squared is 9, and the square root of 9 is 3 Unlike other programming tasks for the labs up until this point, for this part of the lab you will be provided with pseudocode only. No part of the code is being given to you. And, to motivate you using some of the neater features of switch statements that have been shown in lecture, to get full credit: a) Your switch statement must have 9 cases b) Your switch statement can have a default case c) d) Among the 9 cases in your switch statement, you can have only 7 System.out.prinln or System.out.print statements. If you add a default case, you can have an additional print/println statement. Hint: In order to write a switch statement with the above three properties, notice that the integers 3 and 9 have the same neat fact, as well as the integers 7 and 8. Refer to the lecture slides, to see examples of switch statements where the code body for a case does not have a break statement. The pseudocode: 1. Import the Scanner class 2. Create the java class One Through Nine and write the main routine. 3. 4. 5. 6. Declare a variable that will hold the integer that the user types and create a Scanner object. Provide instructions to the user: "Pick a number, 1-9, that you want to know a fun fact about:" Use the nextInt() method of the Scanner class to retrieve the user's input Write a switch statement where the switch condition is the variable that holds the user's input CS110, Tatiana Harrison, CWU, Lab 4: printf and swtich Page 3 of 5 Page 3 of 5 ว ZOOM + 7. Write the switch statement according to the above conditions (a-c) Be sure to test your program. Example invocations of a program that implements the correct solution are shown in Figures 3-6. Pick a number, 1-9, that you want to know a fun fact about: 1 1 can be written as 1*1, thus it has two factors, but they both are not different and hence it is not a prime number Figure 3: Sample output of the program One ThroughNine when the user inputs 1 Pick a number, 1-9, that you want to know a fun fact about: 9 3 squared is 9, and the square root of 9 is 3 Figure 4: Sample output of the program One ThroughNine when the user inputs 9 Pick a number, 1-9, that you want to know a fun fact about: 3 3 squared is 9, and the square root of 9 is 3 Figure 5: Sample output of the program One ThroughNine when the user inputs 3 Pick a number, 1-9, that you want to know a fun fact about: 7 The numbers 7 and 8 are both less than 9. Amazing! Figure 6: Sample output of the program One ThroughNine when the user inputs 7. The same fun fact should be displayed when the user inputs 8. III. What to hand in The following files should be uploaded to Canvas (use the "Add Another File" option to submit multiple files): Decimal MathUsing Integers.java OneThroughNine.java Be sure that each java file is commented and be sure to include your name at the top of each file. If your code does not compile because you've been unable to fix a syntax error, then the comments that you provide in your code will allow you to receive partial credit. Code must also be indented, so that it is easy to read. Finally, make sure that you have given your variables good descriptive names. IV. Rubric File / points Decimal MathUsingIntegers.java compiles and generates correct output when run OneThroughNine.java compiles and generates correct output when run Points 40 40 Both java files are commented, well formatted and contain your name and date Variable names are adequate and descriptive in both java files 10 10 CS110, Tatiana Harrison, CWU, Lab 4: printf and swtich Page 4 of 5 Page < 4 of 5 ว ZOOM + 1 of 5 In this lab, you'll write two Java programs, using the jGRASP Integrated Development Environment (IDE). This lab is worth 100 points. Section III details what needs to be submitted via Canvas. Section IV is the rubric. Preliminaries 1. Using your CWU login information (same as what you use to log into myCWU and Canvas) log into the computer. 2. For this lab, create a folder, and call it lab4. During lab you can save and work on your programs there, but only the java files that you upload to Canvas will be graded. I. The printf method In lecture you have learned about the printf method, that allows you to format output in a variety of ways. For this part of the lab, assume that you are an employee at ACME programming, and you have been given a java compiler that is not working correctly; its next Double () method of the Scanner class is broken and cannot be used. However, assume that the nextInt() method of the Scanner class is working fine. Your boss is not good at performing simple arithmetic calculations in his head; he also is a day-dreamer, and wants to know how much money his bank account would have if its value increased 10,000-fold. He wants you to write a java program that receives as input from the user a decimal value, multiplies that value by 10,000, and outputs the result using 6 decimal places. Hmmm. It seems hopeless, because the nextDouble () method of the Scanner class is not working, so you cannot retrieve a decimal value input using the keyboard. Luckily, you can write a program that can perform decimal arithmetic using only the nextInt() method to receive input from the keyboard. Here's how: you ask the user to input an integer value that is digits to the left of the decimal place of a decimal point, and you ask the user to input a second integer value, that corresponds to the digits to the right of the decimal place of a decimal point. You then divide the integer value that represents the digits to the right of the decimal place by 100, and add that to the integer that represents the digits to the left of the decimal place. Some casting is required. Here are the step by step instructions: Page 1. Start jGRASP, and create a new java file. 2. Type the text that is shown in Figure 1 into the editor panel of the new java file that you just created. Add/modify the comments accordingly, so that your name is listed as an author, and include a description of the program. As always, be generous with comments - they make the code much easier to read. A good rule of thumb is to have at least one comment every 3-5 lines of code. Also CS110, Tatiana Harrison, CWU, Lab 4: printf and swtich Page 1 of 5 use indentation. ว ZOOM + 3. use indentation. Save the java file as Decimal MathUsing Integers.java in your lab4 folder. Compile the program and run it. Make sure that your program outputs what you see in Figure 2. If you have typed any part of the code incorrectly (and introduced a syntax error, you'll have to fix the error, until your code is error-free). //import scanner import java.util.Scanner; public class DecimalMathUsing Integers { //main routine public static void main (String[] args) { //declare variables and assign int wholePart=0, fractionalPart = 0; double composed DecimalValue = 0.0; Scanner keyboard = new Scanner(System.in); //instruct user System.out.print("Input an integer for digits left of the decimal: "); whole Part = keyboard.nextInt(); System.out.print("Input an integer> for digits right of the decimal: "); fractional Part keyboard.nextInt(); //output status message and calculations System.out.println("The whole Part variable has the value: + wholePart); System.out.println("The fractional Part variable has the value: " + fractionalPart); composedDecimalValue = whole Part + fractional Part/100.0; System.out.println("The composed DecimalValue is: " + composed DecimalValue); //mult composed Decimal by 10,000 and format with commas and 6 decimal places System.out.printf("The formatted composed DecimalValue times 10,000 is: %, .6f \n", (composed DecimalValue * 10000.0)); Figure 1: The program DecimalMathUsing Integers ----j GRASP exec: java Decimal MathUsing Integers Input an integer for the digits to the left of a decimal: 41 Input an integer >= 10, for the digits to the right of the decimal: 95 The leftOfDecimalDigits variable has the value: 41 The rightOfDecimalDigits variable has the value: 95 The composed DecimalVal is 41.95 The formatted composed Decimal Val times 10,000 is 419,500.000000 jGRASP: operation complete. Figure 2: Sample output of the code shown in Figure 1. Page 2 of 5 ว ZOOM + II. The switch statement By now, you've seen in lecture many examples of the switch statement. Review those slides if you need a refresher. This part of the lab will help you with the programming portion of homework #3, where you must use a switch statement. For the second part of this lab, you'll write a program, OneThroughNine java, that asks the user to input an integer 1 through 9, and then prints an interesting fact about the selected number. The "interesting" facts about each of the integers that your program must output is shown in the following table: Table 1: Interesting facts about the integers 1 through 9 Interesting fact Integer 1 1 can be written as 1*1, thus it has two factors, but they both are not different and hence it is not a prime number 2 2 is the only prime number that is even and it is the smallest prime number 3 3 squared is 9, and the square root of 9 is 3 4 4 is the smallest square greater than 1 5 5 is a half of 10. Wow! 6 6 is the smallest number that is equal to the sum of its proper divisors. Interesting! 7 The numbers 7 and 8 are both less than 9. Amazing! 8 The numbers 7 and 8 are both less than 9. Amazing! 9 3 squared is 9, and the square root of 9 is 3 Unlike other programming tasks for the labs up until this point, for this part of the lab you will be provided with pseudocode only. No part of the code is being given to you. And, to motivate you using some of the neater features of switch statements that have been shown in lecture, to get full credit: a) Your switch statement must have 9 cases b) Your switch statement can have a default case c) d) Among the 9 cases in your switch statement, you can have only 7 System.out.prinln or System.out.print statements. If you add a default case, you can have an additional print/println statement. Hint: In order to write a switch statement with the above three properties, notice that the integers 3 and 9 have the same neat fact, as well as the integers 7 and 8. Refer to the lecture slides, to see examples of switch statements where the code body for a case does not have a break statement. The pseudocode: 1. Import the Scanner class 2. Create the java class One Through Nine and write the main routine. 3. 4. 5. 6. Declare a variable that will hold the integer that the user types and create a Scanner object. Provide instructions to the user: "Pick a number, 1-9, that you want to know a fun fact about:" Use the nextInt() method of the Scanner class to retrieve the user's input Write a switch statement where the switch condition is the variable that holds the user's input CS110, Tatiana Harrison, CWU, Lab 4: printf and swtich Page 3 of 5 Page 3 of 5 ว ZOOM + 7. Write the switch statement according to the above conditions (a-c) Be sure to test your program. Example invocations of a program that implements the correct solution are shown in Figures 3-6. Pick a number, 1-9, that you want to know a fun fact about: 1 1 can be written as 1*1, thus it has two factors, but they both are not different and hence it is not a prime number Figure 3: Sample output of the program One ThroughNine when the user inputs 1 Pick a number, 1-9, that you want to know a fun fact about: 9 3 squared is 9, and the square root of 9 is 3 Figure 4: Sample output of the program One ThroughNine when the user inputs 9 Pick a number, 1-9, that you want to know a fun fact about: 3 3 squared is 9, and the square root of 9 is 3 Figure 5: Sample output of the program One ThroughNine when the user inputs 3 Pick a number, 1-9, that you want to know a fun fact about: 7 The numbers 7 and 8 are both less than 9. Amazing! Figure 6: Sample output of the program One ThroughNine when the user inputs 7. The same fun fact should be displayed when the user inputs 8. III. What to hand in The following files should be uploaded to Canvas (use the "Add Another File" option to submit multiple files): Decimal MathUsing Integers.java OneThroughNine.java Be sure that each java file is commented and be sure to include your name at the top of each file. If your code does not compile because you've been unable to fix a syntax error, then the comments that you provide in your code will allow you to receive partial credit. Code must also be indented, so that it is easy to read. Finally, make sure that you have given your variables good descriptive names. IV. Rubric File / points Decimal MathUsingIntegers.java compiles and generates correct output when run OneThroughNine.java compiles and generates correct output when run Points 40 40 Both java files are commented, well formatted and contain your name and date Variable names are adequate and descriptive in both java files 10 10 CS110, Tatiana Harrison, CWU, Lab 4: printf and swtich Page 4 of 5 Page < 4 of 5 ว ZOOM + 1 of 5 In this lab, you'll write two Java programs, using the jGRASP Integrated Development Environment (IDE). This lab is worth 100 points. Section III details what needs to be submitted via Canvas. Section IV is the rubric. Preliminaries 1. Using your CWU login information (same as what you use to log into myCWU and Canvas) log into the computer. 2. For this lab, create a folder, and call it lab4. During lab you can save and work on your programs there, but only the java files that you upload to Canvas will be graded. I. The printf method In lecture you have learned about the printf method, that allows you to format output in a variety of ways. For this part of the lab, assume that you are an employee at ACME programming, and you have been given a java compiler that is not working correctly; its next Double () method of the Scanner class is broken and cannot be used. However, assume that the nextInt() method of the Scanner class is working fine. Your boss is not good at performing simple arithmetic calculations in his head; he also is a day-dreamer, and wants to know how much money his bank account would have if its value increased 10,000-fold. He wants you to write a java program that receives as input from the user a decimal value, multiplies that value by 10,000, and outputs the result using 6 decimal places. Hmmm. It seems hopeless, because the nextDouble () method of the Scanner class is not working, so you cannot retrieve a decimal value input using the keyboard. Luckily, you can write a program that can perform decimal arithmetic using only the nextInt() method to receive input from the keyboard. Here's how: you ask the user to input an integer value that is digits to the left of the decimal place of a decimal point, and you ask the user to input a second integer value, that corresponds to the digits to the right of the decimal place of a decimal point. You then divide the integer value that represents the digits to the right of the decimal place by 100, and add that to the integer that represents the digits to the left of the decimal place. Some casting is required. Here are the step by step instructions: Page 1. Start jGRASP, and create a new java file. 2. Type the text that is shown in Figure 1 into the editor panel of the new java file that you just created. Add/modify the comments accordingly, so that your name is listed as an author, and include a description of the program. As always, be generous with comments - they make the code much easier to read. A good rule of thumb is to have at least one comment every 3-5 lines of code. Also CS110, Tatiana Harrison, CWU, Lab 4: printf and swtich Page 1 of 5 use indentation. ว ZOOM +
Expert Answer:
Answer rating: 100% (QA)
Heres the code with proper indentation Java import javautilScanner public class DecimalMathUsingIntegers public static void mainString args int wholePart fractionalPart Scanner keyboard new ScannerSys... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
Find the maximum value of (x, y, z) = x a y b z c for x, y, z 0 on the unit sphere, where a, b, c > 0 are constants.
-
Presented below are the 2018 income statement and comparative balance sheets for Santana Industries. Additional information for the 2018 fiscal year ($ in thousands): 1. Cash dividends of $1,000 were...
-
A study evaluated the results of a two-way ANOVA on the effects of the two factorsexercise price of an option and the time of expiration of an optionon implied interest rates (the measured variable)....
-
Reconsider the case of Tony Angelas, which was used to illustrate expectancy theory. Imagine that you are Tonys boss and you think that he can be motivated to perform at a high level. Suppose you...
-
Langley, Inc. is the manufacturer of lawn care equipment. The company incurs the following costs while manufacturing edgers: Handle and shaft of edger Motor of edger Factory labor for workers...
-
ASSIGNMENT (1) Explain what u understand by a secretary of a company (2) What qualifications will u look for before appointing a person as a secretary (3) What are the duties and functions of a...
-
The Apex Television Company has to decide on the number of 65 and 55 sets to be produced at one of its factories. Market research indicates that at most 280 of the 65 sets and 70 of the 55 sets can...
-
Provide a summary of the facts, main issue/points, rule from the case and how the court decided the following property cases 1. State Ex Rel Thornton v. Hay 2. S.S. Kresge Co v Winkelman Realty Co...
-
1. What action should you take if an engine fails on the runway prior to reaching VMC? 2. Select the true statement regarding the appropriate actions to take after an engine failure in a light...
-
Complete the table found in assignment 5 template with your top values and indicate why they are important to you. Aim for at least 5. Value Why is this important My Reflections What skill sets do...
-
Which tense is used in history reports?Question 8 options:PastPresentFuture
-
One of the key issues involved with big data, predictive analytics, and data mining is that of governance. With the plethora of information available to data analysts, companies are honing their...
-
Choose a real-life company whose business uses innovative methods or technologies. Analyze the business according to the following categories, the system of the Social Business Model Canvas. The...
-
On the Port Coquitlam map, find Simon Fraser University. (Lower left corner.) Give the UTM location for the largest building, i.e. the white center of the building. Answer: On the North Vancouver...
-
If M = 7, s = 2, and X = 9.5, what is z?
-
If Charles, a 16-year-old child model, earns $50,000 a year and is completely self supporting even though he lives with his parents, can his parents claim him as a dependent? Why or why not?...
-
Steve Drake sells a rental house on January 1, 2012, and receives $130,000 cash and a note for $55,000 at 10 percent interest. The purchaser also assumes the mortgage on the property of $45,000....
-
Clarita is a single taxpayer with two dependent children, ages 10 and 12. Clarita pays $3,000 in qualified child care expenses during the year. If her adjusted gross income (all from wages) for the...
-
Outline the general strategy used in metagenomics.
-
The codon change (Gly-12 to Val-12) in human H-ras that converts it to oncogenic H-ras has been associated with many types of cancers. For this reason, researchers would like to develop drugs to...
-
You need to understand the approach described in question 3 in More Genetic TIPS before answering this question. A muscle-specific gene was cloned and then subjected to promoter bashing. As shown...
Study smarter with the SolutionInn App