Docsity

Java Programming: Quiz 3 - Objects, Strings, Random Numbers, Math, and Formatting Output -, Quizzes of Javascript programming

Toggle sidebar
bg1
Materials for Quiz 3
1. Creating objects
•Creating object reference variable
A class name can be used as a type to declare an object reference variable
String title;
// String is a class name
// title is declared as String reference variable
•Creating object
Generally, we use the new operator to create an object
title = new String (“Java Programming”);
// String (“Java Programming”) is String constructor, which is
// a special method that sets up the object
// To create a String object, there is another way to do it:
// title = “Java Programming”;
•Invoking methods
use the dot operator to invoke its methods
count = title.length();
//length() is method of String object, title
•References
An object reference can be thought of as a pointer to the location of the object
String name1 = “Tom”;
String name2 = “Mike”;
System.out.println(“name1: ” + name1 + “ name2: ” + name2);
//name1: Tom name2: Mike
name2 = name1;
// name2 points to the object which name1 points to
System.out.println(“name1: ” + name1 + “ name2: ” + name2);
//name1: Tom name2: Tom
// String object “Mike” has no object reference
// variable pointing to it and is ready to be
// garbage collected by java compiler automatically
name1 = “Peter”;
System.out.println(“name1: ” + name1 + “ name2: ” + name2);
//name1: Peter name2: Tom
2. String Class
•Two ways to create String object
//with new
String title = new String (“Java Programming”);
or
//without new
String title = “Java Programming”;
pf3
pf4
pf5

Partial preview of the text

Download Java Programming: Quiz 3 - Objects, Strings, Random Numbers, Math, and Formatting Output - and more Quizzes Javascript programming in PDF only on Docsity!

Materials for Quiz 3

  1. Creating objects
    • Creating object reference variable A class name can be used as a type to declare an object reference variable String title; // String is a class name // title is declared as String reference variable
    • Creating object Generally, we use the new operator to create an object title = new String (“Java Programming”); // String (“Java Programming”) is String constructor , which is // a special method that sets up the object // To create a String object, there is another way to do it: // title = “Java Programming”;
    • Invoking methods use the dot operator to invoke its methods count = title.length(); //length() is method of String object, title
    • References An object reference can be thought of as a pointer to the location of the object String name1 = “Tom”; String name2 = “Mike”; System.out.println(“name1: ” + name1 + “ name2: ” + name2); //name1: Tom name2: Mike name2 = name1; // name2 points to the object which name1 points to System.out.println(“name1: ” + name1 + “ name2: ” + name2); //name1: Tom name2: Tom // String object “Mike” has no object reference // variable pointing to it and is ready to be // garbage collected by java compiler automatically name1 = “Peter”; System.out.println(“name1: ” + name1 + “ name2: ” + name2); //name1: Peter name2: Tom
  2. String Class
    • 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’
  1. Random Class
  • Import util class import java.util.Random; or //use wildcard character, * import java.util.*; // When starting to write programs, 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; // more challenging //generate a random number of -13, -9, -5,…, 59 randomeNumber = 4 * generator.nextInt(19) - 13;
  1. Wrapper Class The java.lang package contains wrapper classes that correspond to each primitive type Primitive Type Wrapper Class byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean void Void // 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; //Create an Integer object Integer iobj = new Integer(i); //autoboxing Integer bObj = 25; // Integer object bObj whose int value is 25 // deboxing i = bObj; // i: 25
  2. Interactive Programs The Scanner class provides convenient methods for reading input values of various types. (1) Import Scanner class import java.util.Scanner; (2) Create a Scanner object with keyboard as input device Scanner scan = new Scanner(System.in); (3) Call methods belonging to Scanner class to return data which you want String s = scan.next();//return a string int i = scan.nextInt();//return an integer double d = scan.nextDouble();//return a double For methods in Scanner class, please check java doc at http://java.sun.com/j2se/1.5.0/docs/api/ java/util/Scanner.html

Online javadoc site http://java.sun.com/j2se/1.5.0/docs/api/index.html Please try some methods out. Keep in mind, when you expect user to input something, you should give a user a proper prompt!

  • Please refer Examples 3.18, 3.19 in Chapter 3 of your textbook.