Docsity

Study Guide for Exam One - Java Programming | COP 2250, Study notes of Javascript programming

Toggle sidebar
bg1
Materials for Exam One
1. Identifiers and reserved words
An identifier can be made up of letters, digits, the underscore character ( _ ), and the dollar sign and
cannot begin with a digit.
By convention, programmers use different case styles for different types of identifiers, such as
title case for class names - String
upper case for constants - MAXIMUM
Reserved words: public, class, static, void, return, int, float, double, float, short, byte and so on.
2. Data conversion
•In Java, data conversions can occur in three ways:
–assignment conversion
float f;
int i = 5;
f = i; // f is 5.0
–promotion
float f = 2.0f;
// or 2.0F since 2.0 is a double constant
// float f = 2.0; wrong!
int i = 5;
f = f/i;
// the value of i is promoted to float type and
// then the division between two float type data
//is performed
–casting
float f = 2.0f;
int i = 5;
i = (int)f/i;
//since the expression of “f/i” will return a
//float type data which could not be
//automatically converted to integer and casting
//is required
//Casting: a pair of parenthesis with casting //type enclosed
such as (int) (float)
•Widening conversions: from small data type to a larger one
short -> int -> long
float -> double
int -> float
long -> double
Reverse arrows correspond to narrowing conversions
Widening conversions are safe and could be done automatically.
int i = 5;
float f;
f = i; // int -> float
pf3
pf4
pf5
pf8

Partial preview of the text

Download Study Guide for Exam One - Java Programming | COP 2250 and more Study notes Javascript programming in PDF only on Docsity!

Materials for Exam One

  1. Identifiers and reserved words An identifier can be made up of letters, digits, the underscore character ( _ ), and the dollar sign and cannot begin with a digit. By convention, programmers use different case styles for different types of identifiers, such as title case for class names - String upper case for constants - MAXIMUM Reserved words: public, class, static, void, return, int, float, double, float, short, byte and so on.
  2. Data conversion
    • In Java, data conversions can occur in three ways:
      • assignment conversion float f; int i = 5; f = i; // f is 5.
      • promotion float f = 2.0f; // or 2.0F since 2.0 is a double constant // float f = 2.0; wrong! int i = 5; f = f/i; // the value of i is promoted to float type and // then the division between two float type data //is performed
      • casting float f = 2.0f; int i = 5; i = (int)f/i; //since the expression of “f/i” will return a //float type data which could not be //automatically converted to integer and casting //is required //Casting: a pair of parenthesis with casting //type enclosed such as (int) (float)
    • Widening conversions: from small data type to a larger one short -> int -> long float -> double int -> float long -> double Reverse arrows correspond to narrowing conversions Widening conversions are safe and could be done automatically. int i = 5; float f; f = i; // int -> float

Narrowing conversions need to be done through casting! i = f; // wrong assignment. Correct one is: i = (float)f and the value of i is the integer part of variable f. Try to understand: int total = 20, count = 7; float average; average = total / count; // 2. average = (float)total / count; // 2. average = total / (float)count; // 2. average = (float)(total / count); // 2. //casting in the last statement is redundant since //int->float is a widening conversion and could be //converted automatically

  1. Assignment Operator
  2. Expression Evaluation int x = 4,y = 5,z=3; String s = "Hello"; System.out.println(x + z + s + x + z); 7Hello x + z + s + x + z consists of only + operator and expression should be evaluated from left to right. Step I: x, z are both integers and x + z is 7 Step II: For 7 + s, s is a String object and the + means String concatenation. 7 + s is “7Hello”

Operator

Example

x += y

x -= y

x *= y

x /= y

x %= y

Equivalent To

x = x + (y)

x = x – (y)

x = x * (y)

x = x / (y)

x = x % (y)

int i = 10; i /= 5 + 2; // value i =

String s = “Hello”;

s += 6 + 6; // value s = Hello12 because s = s + (6 + 6)

1 because i = i/(5 + 2)

  • Two ways to create String object //with new String title = new String (“Java Programming”); or //without new String title = “Java Programming”;
  • String methods The index begins at zero in each string In the string "Hello", the character 'H' is at index 0 and the 'o' is at index 4 length() – return the length of the string Each escape character corresponds to one character String s = new String(“Hello\tWorld!\n”); System.out.println( s.length() ); // substring(start, end) – return substring of the string object Start – inclusive ; end – exclusive String s = new String(“Hello\tWorld!\n”); System.out.println( s.substring(2, 5) ); //llo; 2 corresponds to first l and 5 to ‘\t’
  • String <-> int // get int from a string String s = “375”; int sVal = Integer.parseInt(s); // get string of characters from a int variable int i = 345; String s = “” + i;
  1. Random Class
  • Import util class import java.util.Random; or //use wildcard character, * import java.util.*; // it is recommended to avoid using wildcard character
  • Creating Random object Random generator = new Random();
  • Generate a random number int randomNumber; //generate a random number of 0, 1, 2, 3, 4, 5 randomeNumber = generator.nextInt(6);

//generate a random number of 2, 3, 4, …, 11 randomeNumber = generator.nextInt(10) + 2; //generate a random number of -6, -4, -2, …, 24 randomeNumber = 2 * generator.nextInt(16) - 6; //generate a random number of -13, -11, -9, …, 57 randomeNumber = 2 * generator.nextInt(36) - 13;

  1. Math Class
    • Invoking methods The methods of the Math class are static methods Static methods can be invoked through the class name – no object of the Math class is needed value = Math.cos(90) + Math.sqrt(delta); // abs(arg) – absolute value of arg // sqrt(arg) – square root of arg // exp(arg) – exponential value of arg // pow(x, y) – x to y power
  2. Formatting output
    • NumberFormat //import class import java.text.NumberFormat; //create formatter object //Currency Formatter object NumberFormat currencyFmt = NumberFormat.getCurrencyInstance(); // Percent Formatter object NumberFormat percentFmt = NumberFormat.getPercentInstance(); // Each formatter object has a method called format // that returns a string with the specified // information in the appropriate format double findal TAX_RATE = 0.06; System.out.println(“Tax Rate: ” + percentFmt.format(TAX_RATE)); //Tax Rate: 6%
    • DecimalFormat //import class import java.text.DecimalFormat; //create DecimalFormat object DecimalFormat fmt = new DecimalFormat ("0.###"); //0 Digit // # Digit, zero shows as absent System.out.println(fmt.format(3.8976)); //3. System.out.println(fmt.format(3.8)); //3.
  3. What is the output of the following segment?
  • Write a while loop to input Strings using methods from Scanner class
  • Prompt the user whether to enter another String (no/n stop)
  • Accumulate the Strings in a String variable out with a space between the Strings and print out after the while loop import java.util.Scanner; public class test1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String output = ""; String temp = "y"; while (temp.equalsIgnoreCase("yes") || temp.equalsIgnoreCase("y")) { System.out.println("Please enter a string >"); output += scan.next(); System.out.print("Do you want to enter another one (yes/no)?"); temp = scan.next(); } System.out.println(output); } }
  1. Write a loop to input numbers (ints) using methods from Scanner class until the user enter -1. Sum the numbers in an int variable sum. Finally, print out the sum value of all numbers except the last number, -1. import java.util.Scanner; public class test1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int sum = 0; System.out.println("Please enter an integer (-1 to stop)

"); int input = scan.nextInt(); while (input != -1) { sum += input; System.out.println("Please enter an integer (-1 to stop) > "); input = scan.nextInt();

System.out.println("Sum is: " + sum); } }