William Hellems-Moody
@willhm9479njjsmyzw• Oct 30, 2022
Javanotes 9, Section 2.3 -- Strings, Classes, Objects, and Subroutines
They are used to describe objects.
object is a collection of variables and subroutines.
The String class defines a lot of functions. Here are some that you might find useful. Assume that s1 and s2 are variables of type String:
Javanotes 9, Section 2.3 -- Strings, Classes, Objects, and Subroutinesmath.hws.edu
William Hellems-Moody
@willhm9479njjsmyzw• Jun 27, 2022
Javanotes 9, Section 9.3 -- Stacks, Queues, and ADTs
import java.util.Arrays; // For the Arrays.copyOf() method. public class StackOfInts { // (alternate version, using an array) private int[] items = new int[10]; // Holds the items on the stack. private int top = 0; // The number of items currently on the stack. /** * Add N to the top of the stack. */ public void push( int N ) { if (top == items.length) { // The array is full, so make a new, larger array and // copy the current stack items into it. items = Arrays.copyOf( items, 2*items.length ); } items[top] = N; // Put N in next available spot. top++; // Number of items goes up by one. } /** * Remove the top item from the stack, and return it. * Throws an IllegalStateException if the stack is empty when * this method is called. */ public int pop() { if ( top == 0 ) throw new IllegalStateException("Can't pop from an empty stack."); int topItem = items[top - 1]; // Top item in the stack. top--; // Number of items on the stack goes down by one. return topItem; } /** * Returns true if the stack is empty. Returns false * if there are one or more items on the stack. */ public boolean isEmpty() { return (top == 0); }
Javanotes 9, Section 9.3 -- Stacks, Queues, and ADTsmath.hws.edu
William Hellems-Moody
@willhm9479njjsmyzw• Jun 3, 2022
Java Programming Masterclass covering Java 11 & Java 17 | Udemy
Section 6: OOP Part 1 - Classes, Constructors and Inheritance
Section 7: OOP Part 2 - Composition, Encapsulation, and Polymorphism
Section 8: Arrays, Java inbuilt Lists, Autoboxing and Unboxing
Java Programming Masterclass covering Java 11 & Java 17 | Udemywww.udemy.com
William Hellems-Moody
@willhm9479njjsmyzw• Jun 2, 2022
Javanotes 9, Section 5.1 -- Objects, Instance Methods, and Instance Variables
"constructor;" the constructor is a subroutine that creates objects.
object that is created using a class is said to be an instance of that class.
Javanotes 9, Section 5.1 -- Objects, Instance Methods, and Instance Variablesmath.hws.edu
William Hellems-Moody
@willhm9479njjsmyzw• Jun 2, 2022
Javanotes 9, Section 3.7 -- Introduction to Exceptions and try..catch
try { statements-1 } catch ( exception-class-name variable-name ) { statements-2 }
When the computer executes this try..catch statement, it executes statements-1, the statements inside the try part. If no exception occurs during the execution of statements-1, then the computer just skips over the catch part and proceeds with the rest of the program.
example, suppose that str is a variable of type Stringwhose value might or might not represent a legal real number. Then we could say:
Javanotes 9, Section 3.7 -- Introduction to Exceptions and try..catchmath.hws.edu
William Hellems-Moody
@willhm9479njjsmyzw• Jun 2, 2022
Javanotes 9, Section 3.5 -- The if Statement
if (boolean-expression) statement-1 else statement-2
You can force the computer to use the other interpretation by enclosing the nested if in a block: if ( x > 0 ) { if (y > 0) System.out.println("First case"); } else System.out.println("Second case"); These two if statements have different meanings: In the case when x <= 0, the first statement doesn't print anything, but the second statement prints "Second case".
In my final program, I decided to make things more interesting by allowing the user to repeat the process of entering a measurement and seeing the results of the conversion for each measurement. The program will end only when the user inputs 0. To program that, I just had to wrap the above algorithm inside a while loop, and make sure that the loop ends when the user inputs a 0. Here's the complete program: import textio.TextIO; /** * This program will convert measurements expressed in inches, * feet, yards, or miles into each of the possible units of * measure. The measurement is input by the user, followed by * the unit of measure. For example: "17 feet", "1 inch", or * "2.73 mi". Abbreviations in, ft, yd, and mi are accepted. * The program will continue to read and convert measurements * until the user enters an input of 0. */ public class LengthConverter { public static void main(String[] args) { double measurement; // Numerical measurement, input by user. String units; // The unit of measure for the input, also // specified by the user. double inches, feet, yards, miles; // Measurement expressed in // each possible unit of // measure. System.out.println(""" Enter measurements in inches, feet, yards, or miles. For example: 1 inch 17 feet 2.73 miles You can use abbreviations: in ft yd mi I will convert your input into the other units of measure. """); while (true) { /* Get the user's input, and convert units to lower case. */ System.out.print("Enter your measurement, or 0 to end: "); measurement = TextIO.getDouble(); if (measurement == 0) break; // Terminate the while loop. units = TextIO.getlnWord(); units = units.toLowerCase(); // convert units to lower case /* Convert the input measurement to inches. */ if (units.equals("inch") || units.equals("inches") || units.equals("in")) { inches = measurement; } else if (units.equals("foot") || units.equals("feet") || units.equals("ft")) { inches = measurement * 12; } else if (units.equals("yard") || units.equals("yards") || units.equals("yd")) { inches = measurement * 36; } else if (units.equals("mile") || units.equals("miles") || units.equals("mi")) { inches = measurement * 12 * 5280; } else { System.out.println("Sorry, but I don't understand \"" + units + "\"."); continue; // back to start of while loop } /* Convert measurement in inches to feet, yards, and miles. */ feet = inches / 12; yards = inches / 36; miles = inches / (12*5280); /* Output measurement in terms of each unit of measure. */ System.out.printf(""" That's equivalent to: %14.5g inches %14.5g feet %14.5g yards %14.5g miles """, inches, feet, yards, miles); System.out.println(); } // end while System.out.println(); System.out.println("OK! Bye for now."); } // end main() } // end class LengthConverter
Javanotes 9, Section 3.5 -- The if Statementmath.hws.edu
William Hellems-Moody
@willhm9479njjsmyzw• Jun 2, 2022
Javanotes 9, Section 3.4 -- The for Statement
3.4.1 For Loops
for ( N = 10 ; N >= 1 ; N-- ) System.out.println( N );
for ( N = 0 ; N < 10 ; N++ ) System.out.println( N );
Javanotes 9, Section 3.4 -- The for Statementmath.hws.edu
William Hellems-Moody
@willhm9479njjsmyzw• Jun 2, 2022
Javanotes 9, Section 3.1 -- Blocks, Loops, and Branches
The sixcontrol structures are: the block, thewhile loop, the do..while loop, the for loop, the if statement, and the switch statement.
format of a block is: { statements }
possible for a block to contain no statements at all; such a block is called an empty block
Javanotes 9, Section 3.1 -- Blocks, Loops, and Branchesmath.hws.edu
William Hellems-Moody
@willhm9479njjsmyzw• Jun 2, 2022
Javanotes 9, Section 2.5 -- Details of Expressions
ternary operator
three operands—andit comes in two pieces, ? and :, that have to be used together. It takesthe form
boolean-expression ? expression1 : expression2
Javanotes 9, Section 2.5 -- Details of Expressionsmath.hws.edu
William Hellems-Moody
@willhm9479njjsmyzw• Jun 2, 2022
Javanotes 9, Section 2.4 -- Text Input and Output
Text Input and Output
This section explains how to get data from the user, and it covers output in more detail than we have seen so far. It also has a section on using files for input and output.
The function System.out.printf can be used to produce formattedoutput.
Javanotes 9, Section 2.4 -- Text Input and Outputmath.hws.edu