Gaddis Extra Notes For Ch2


Reading a Character

posted Jan 20, 2014, 12:35 AM by iman baqir   [ updated Jan 20, 2014, 12:35 AM by Karwan Jacksi ]

Sometimes you will want to read a single character from the keyboard. For example, your program might ask the user a yes/no question, and specify that he or she type Y for yes or N for no. The Scanner class does not have a method for reading a single character, however. The approach that we will use in this book for reading a character is to use the Scanner class's nextLine method to read a string from the Keyboard, and then use the String class's charAt method to extract the first character of the string. This will be the character that the user entered at the keyboard, I lere is an example:

 

String  input;                           // To hold a line of input

char     answer;                        // To hold a single character

 

/ / Create a Scanner object for keyboard input.

Scanner keyboard = new Scanner(System.in);

 

/ / Ask the user a question.

System.out.print("Are you having fun? (Y=yes, H=no) ";

input = keyboard.nextLine();            // Get a line of input,

answer = input.charAt(0);                 // Get the first character.

 

The input variable references a String object. The last statement in this code calls the String class's charAt method to retrieve the character at position 0, which is the first character in the string. After this statement executes, the answer variable will hold the character that the user typed at the keyboard.

Some of the scanner class methods

posted Jan 20, 2014, 12:32 AM by iman baqir   [ updated Jan 20, 2014, 12:32 AM by Karwan Jacksi ]

Method                          Example and Description

nextByte                        Example Usage:

byte x;

Scanner keyboard • new Scanner(System.in);

System.out.print("Enter a byte value: ");

x = keyboard.nextBytef();

Description: Returns input as a byte.

 

 

nextDoubie                    Example Usage:

double number;

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a double value: " );

number = keyboard.nextDoubie();

Description: Returns input as a double.

 

 

nextFloat                       Example Usage:

float number;

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a float value: ");

number = keyboard.nextFloa();

Description: Returns input as a float .

 

 

Nextlnt                           Example Usage:

int number;

Scanner keyboard = new Scanner(System.in);

System.out.print(“Enter an integer value: “);

number = keyboard.nextlnt();

Description: Returns input as an int;

 

 

ncxtLinc                         Example Usage:

String name;

Scanner keyboard = new Scanner(System.in);

System.out,print(“Enter your name: “);

name = keyboard.ncxtLinc();

Description: Returns input as a string.

 

                                     

nextLong                        Example Usage:

long number;

Scanner keyboard = new Scanner(System.in);

System.out.print(“Enter a long value: “);

number = keyboard.nextLongf();

Description: Returns input as a long.

 

 

nextShort                       Example Usage:

short number;

Scanner keyboard = new Scanner(Systcm.in);

System.out.print(“Enter a short value: “);

number = keyboard.nextShort();

Description: Returns input as a short.

 

A few string class methods

posted Jan 20, 2014, 12:31 AM by iman baqir   [ updated Jan 20, 2014, 12:31 AM by Karwan Jacksi ]

Method                          Description and Example

charAt(index)                 The argument index is an int value and specifies a character position in the string. The first character is at position 0, the second character is at position I, and so forth. The method returns the character at the specified position. The return value is of the type char.

                                      Example:

char letter;

String name=  "Herman";

Letter = nam.charAt(3);

After this code executes, the variable letter will hold the character ‘m’.

 

 

Length()                         This method returns the number of characters in the string. The return value is of the type int.

                                      Example:

int stringSize;

String name - "Herman";

stringSize - name.length();

After this code executes, the atringSize variable will hold the value 6.

 

         

toLowerCasc()               This method returns a new string that is the lowercase equivalent of the string contained in the calling object.

Example:

String bigName = "HERMAN";

String littleName = bigNamc.toLowerCasc();

After this code executes, the object referenced by littleName will hold the string “herman”.

 

toUpperCase()               This method returns A new string that is the uppercase equivalent of the string contained in the calling object.

 

 

Example:

String littleName = "herman";

String bigName = littleName,toUpperCaae();

After this code executes, the object referenced by bigName will hold the string “HERMAN”.

Cast Operators

posted Jan 20, 2014, 12:19 AM by iman baqir   [ updated Jan 20, 2014, 12:23 AM by Karwan Jacksi ]

The cast operator lets you manually convert a value, even if it means that a narrowing conversion will take place. Cast operators are unary operators that appear as a data type name enclosed in a set of parentheses. The operator precedes the value being converted.

Here is an example:

x =(int)numbcr;

The cast operator in this statement is the word int inside the parentheses, It returns the value in number, converted co an int. This converted value is then stored in x. If number were a floating-point variable, such as a float or a double, the value that is returned would be truncated, which means the fractional part of the number is lost. The original value in the number variable is not changed, however.

 

several statements using cast operators.

Statement                                  Description

littleNum = (short)bigNum;     The cast operator returns the value in bigNum, converted to a short. The converted value is assigned to the                                                            variable littleNum.


x = (long)3.7;                           The cast operator is applied to the expression 3.7. The operator returns the value J, which is assigned to the                                                         variable x.

 

number = (int)72.567;             The cast operator is applied to the expression 72.567.  The operator returns 72, which is used to initialize the                                                         variable number.

 

value = (float)x;                       The cast operator returns the value in x, converted to a float. The converted value is assigned to the variable                                                         value.

 

value = (byte)number;             The cast operator returns the value in number, converted to a byte. The converted value is assigned to the                                                             variable value.

Conversion between Primitive Data Types

posted Jan 19, 2014, 11:57 PM by iman baqir   [ updated Jan 19, 2014, 11:57 PM by Karwan Jacksi ]

The numeric data types in order of their rank can be found in PDF bellow

A list of variable names and tells whether each is legal or illegal in Java.

posted Jan 19, 2014, 11:48 PM by iman baqir   [ updated Jan 20, 2014, 12:12 AM by Karwan Jacksi ]

Variable Name              Legal or Illegalr


dayOfWeek                    legal

 3dGraph                        Illegal because identifiers cannot begin with a digit

 junel997                         legal

 mixture#3                      Illegal because identifiers may use only alphabetic

                                      letters, digits, underscores, or dollar signs

 week day                       Illegal because identifiers cannot contain spaces

Relationship among the System class, the out object, and the print and println methods

posted Jan 19, 2014, 11:45 PM by iman baqir   [ updated Jan 19, 2014, 11:45 PM by Karwan Jacksi ]

The Relationship among the System class, the out object, and the print and println methods can be found from the PDF in the bellow of this page

1-7 of 7