Class Constants Java Program

User Generated

ZR11B

Programming

Description

  • Write the java program 
  • Your program must use class constants (e.g., static final int SIZE=10) in place of any numeric constants (except for 0 and 1), and in place of any character constants.
  • Your program must use nested loops.
  • At least one doubly-nested loop (a for loop inside a for loop inside a for loop).
  • Finally, your program should have a class constant called SIZE such that if the we change its value, the entire size of your drawing will change proportionately. For instance, if your program produces a drawing that's 100 characters wide and 200 long when SIZE = 10, and if we change SIZE to 5 (and make NO other changes), it should produce something about 50 characters wide and 100 characters long. If we change the SIZE to 20, it should produce something like 200 characters wide and 400 characters long, and so on. You may include a comment that specifies what ranges of values constitute valid values for this SIZE constant, but it should be a nontrivial range (that is, don't say that it only works for SIZE = 6, or something like that).

Unformatted Attachment Preview

Building Java Programs Chapter 2 bug Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. 2 An Insect Software Flaw 3 4 Bug, Kentucky Bug Eyed 5 Cheesy Movie 6 Punch Buggy Red … no punchbacks 7 8 BUG 9 10 Data types Java's primitive types • type: A category or set of data values. • primitive types: 8 simple types for numbers, text, etc. – Constrains the operations that can be performed on data – Many languages ask the programmer to specify types – Java also has object types, which we'll talk about later Name – Examples: integer, real number, string • Internally, computers store everything as 1s and 0s 104 à 01101000 "hi" à 01101000110101 Description Examples – int integers – double real numbers – char single text characters 'a', 'X', '?', '\n' – boolean logical values true, false (up to 231 (up to 10308) - 1) 42, -3, 0, 926394 3.1, -0.25, 9.4e3 • Why does Java distinguish integers vs. real numbers? 11 12 Expressions Arithmetic operators • expression: A value or operation that computes a value. • Examples: • operator: Combines multiple values or expressions. –+ ––* –/ –% 1 + 4 * 5 (7 + 2) * 6 / 3 42 – The simplest expression is a literal value. – A complex expression can use operators and parentheses. addition subtraction (or negation) multiplication division modulus (a.k.a. remainder) • As a program runs, its expressions are evaluated. – 1 + 1 evaluates to 2 – System.out.println(3 * 4); prints 12 • How would we print the text 3 * 4 ? 13 Integer division with / Integer remainder with % • When we divide integers, the quotient is also an integer. – 14 / 4 is 3, not 3.5 3 4 ) 14 12 2 4 10 ) 45 40 5 14 52 27 ) 1425 135 75 54 21 • The % operator computes the remainder from integer division. – 14 % 4 is 2 What is the result? – 218 % 5 is 3 3 4 ) 14 12 2 • More examples: – 32 / 5 is 6 – 84 / 10 is 8 – 156 / 100 is 1 – Dividing by 0 causes an error when your program runs. 43 5 ) 218 20 18 15 3 45 % 6 2 % 2 8 % 20 11 % 0 • Applications of % operator: 15 – Obtain last digit of a number: 230857 % 10 is 7 – Obtain last 4 digits: 658236489 % 10000 is 6489 – See whether a number is odd: 7 % 2 is 1, 42 % 2 is 0 16 Precedence Precedence examples • precedence: Order in which operators are evaluated. – Generally operators evaluate left-to-right. 1 - 2 - 3 is (1 - 2) - 3 which is -4 •1 * 2 + 3 * 5 % 4 • \_/ | 2 + 3 * 5 % 4 • \_/ | 2 + 15 % 4 • \___/ | 2 + 3 • \________/ | 5 – But * / % have a higher level of precedence than + 1 + 3 * 4 6 + 8 / 2 * 3 6 + 4 * 3 6 + 12 is 13 is 18 – Parentheses can force a certain order of evaluation: (1 + 3) * 4 is 16 – Spacing does not affect order of evaluation 1+3 * 4-2 is 11 n n n n n 1 + 8 % 3 * 2 - 9 \_/ | 1 + 2 * 2 - 9 \___/ | 1 + 4 - 9 \______/ | 5 - 9 \_________/ | -4 17 18 Real numbers (type double) Precedence questions • Examples: 6.022 , -42.0 , 2.143e17 • What values result from the following expressions? – Placing .0 or . after an integer makes it a double. –9 / 5 – 695 % 20 • The operators + - * / % () all still work with double. –7 + 6 * 5 –7 * 6 + 5 – / produces an exact answer: 15.0 / 2.0 is 7.5 – 248 % 100 / 5 – Precedence is the same: () before * / % before + - –6 * 3 - 9 / 4 – (5 - 7) * 4 – 6 + (18 % (17 - 12)) 19 20 Mixing types Real number example • When int and double are mixed, the result is a double. • 2.0 * 2.4 + 2.25 * 4.0 / 2.0 • \___/ | 4.8 + 2.25 * 4.0 / 2.0 • \___/ | 4.8 + 9.0 / 2.0 • \_____/ | 4.8 + 4.5 • \____________/ | 9.3 – 4.2 * 3 is 12.6 • The conversion is per-operator, affecting only its operands. – 7 / 3 * 1.2 + 3 / 2 – \_/ | 2 * 1.2 + 3 / 2 – \___/ | 2.4 + 3 / 2 – \_/ | 2.4 + 1 – \________/ | 3.4 21 – 3 / 2 is 1 above, not 1.5. • 2.0 + 10 / 3 * 2.5 - 6 / 4 • \___/ | 2.0 + 3 * 2.5 - 6 / 4 • \_____/ | 2.0 + 7.5 - 6 / 4 • \_/ | 2.0 + 7.5 1 • \_________/ | 9.5 1 • \______________/ | 8.5 22 String concatenation • string concatenation: Using + between a string and another value to make a longer string. "hello" + 42 1 + "abc" + 2 "abc" + 1 + 2 1 + 2 + "abc" "abc" + 9 * 3 "1" + 1 4 - 1 + "abc" is is is is is is is "hello42" "1abc2" "abc12" "3abc" "abc27" "11" "3abc" Variables • Use + to print a string and an expression's value together. – System.out.println("Grade: " + (95.1 + 71.9) / 2); • Output: Grade: 83.5 23 24 Receipt example Variables What's bad about the following code? • variable: A piece of the computer's memory that is given a name and type, and can store a value. public class Receipt { public static void main(String[] args) { // Calculate total owed, assuming 8% tax / 15% tip System.out.println("Subtotal:"); System.out.println(38 + 40 + 30); System.out.println("Tax:"); System.out.println((38 + 40 + 30) * .08); System.out.println("Tip:"); System.out.println((38 + 40 + 30) * .15); System.out.println("Total:"); System.out.println(38 + 40 + 30 + (38 + 40 + 30) * .08 + (38 + 40 + 30) * .15); } } – Like preset stations on a car stereo, or cell phone speed dial: – Steps for using a variable: – The subtotal expression (38 + 40 + 30) is repeated – So many println statements 25 • Declare it - state its name and type • Initialize it - store a value into it • Use it - print it or use it as part of an expression Declaration 26 Assignment • variable declaration: Sets aside memory for storing a value. • assignment: Stores a value into a variable. – Variables must be declared before they can be used. – The value can be an expression; the variable stores its result. • Syntax: • Syntax: type name; name = expression; • The name is an identifier. – int x; – double myGPA; x – int x; x = 3; myGPA – double myGPA; myGPA = 1.0 + 2.25; 27 x myGPA 3 3.25 28 Using variables Declaration/initialization • Once given a value, a variable can be used in expressions: int x; x = 3; System.out.println("x is " + x); // x is 3 System.out.println(5 * x - 1); // 5 * 3 - 1 • A variable can be declared/initialized in one statement. • Syntax: type name = value; • You can assign a value more than once: x int x; x = 3; System.out.println(x + " here"); 11 3 – double myGPA = 3.95; myGPA 3.95 // 3 here x = 4 + 7; System.out.println("now x is " + x); // now x is 11 – int x = (11 % 3) + 12; x 14 29 Assignment and algebra Assignment and types • Assignment uses = , but it is not an algebraic equation. = • A variable can only store a value of its own type. – int x = 2.5; means, "store the value at right in variable at left" – The value is converted into the equivalent real number. – double myGPA = 4; • What happens here? // ??? // ERROR: incompatible types • An int value can be stored in a double variable. • The right side expression is evaluated first, and then its result is stored in the variable at left. int x = 3; x = x + 2; 30 x 3 5 – double avg = 11 / 2; myGPA 4.0 avg 5.0 • Why does avg store 5.0 and not 5.5 ? 31 32 Compiler errors Printing a variable's value • Use + to print a string and a variable's value on one line. • A variable can't be used until it is assigned a value. – int x; System.out.println(x); – double grade = (95.1 + 71.9 + 82.6) / 3.0; System.out.println("Your grade was " + grade); // ERROR: x has no value int students = 11 + 17 + 4 + 19 + 14; System.out.println("There are " + students + " students in the course."); • You may not declare the same variable twice. – int x; int x; // ERROR: x already exists • Output: – int x = 3; int x = 5; Your grade was 83.2 There are 65 students in the course. // ERROR: x already exists • How can this code be fixed? 33 34 Receipt question Receipt answer Improve the receipt program using variables. public class Receipt { public static void main(String[] args) { // Calculate total owed, assuming 8% tax / 15% tip int subtotal = 38 + 40 + 30; double tax = subtotal * .08; double tip = subtotal * .15; double total = subtotal + tax + tip; public class Receipt { public static void main(String[] args) { // Calculate total owed, assuming 8% tax / 15% tip System.out.println("Subtotal:"); System.out.println(38 + 40 + 30); System.out.println("Tax:"); System.out.println((38 + 40 + 30) * .08); System.out.println("Tip:"); System.out.println((38 + 40 + 30) * .15); } } System.out.println("Total:"); System.out.println(38 + 40 + 30 + (38 + 40 + 30) * .15 + (38 + 40 + 30) * .08); } 35 } System.out.println("Subtotal: " + subtotal); System.out.println("Tax: " + tax); System.out.println("Tip: " + tip); System.out.println("Total: " + total); 36 Repetition with for loops • So far, repeating a statement is redundant: System.out.println("Homer says:"); System.out.println("I am so smart"); System.out.println("I am so smart"); System.out.println("I am so smart"); System.out.println("I am so smart"); System.out.println("S-M-R-T... I mean S-M-A-R-T"); The for loop • Java's for loop statement performs a task many times. System.out.println("Homer says:"); for (int i = 1; i
Purchase answer to see full attachment
Explanation & Answer:
1 program
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

The files below contain complete work of your assignment. Kindly l...


Anonymous
This is great! Exactly what I wanted.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags