Download presentation
Presentation is loading. Please wait.
Published byAlexander Bennett Modified over 9 years ago
1
Constants A constant is a variable whose value is expected to remain the same throughout a program It is considered “good programming style” to use constants for certain variables – for instance, interest rate on a loan, pi, etc Constants are always written in uppercase To declare a constant, add the word final: final double PI= 3.1415; final int HOURS_IN_A_DAY= 24; PI = 3.7; // this would cause an error
2
Class Libraries and Packages A class library is a set of packages, each of which contains classes (files) of code that can be used by programmers You can use this code simply by importing a package into your program, with a single line of code Top line of code: import library.package.class This is why Object-Oriented languages like Java are so popular – you can use code someone else has written, over and over again
3
Double precision problem In Java, doubles are slightly imprecise. For example, sometimes if you multiply 6.0 by 3.0, the answer is 17.999999999, or 18.00000000001. So, if your program requires precision, either do not use doubles, or, use them but round them off to a certain amount of decimal places. Demo: DecimalRound
4
Working with Strings Characters in a String can be identified by their index #. The first character in a String has index # 0. You need to know the following String methods: Length( ) returns the number of characters in the String charAt(index) returns the char at the given index indexOf(char) reverse of charAt. returns the first index # of the given char substring(Start, End) returns the chars from index Start to index ( End – 1) substring(Start) when only the starting index is given, returns the chars from start to the end of the String toUpperCase, toLowerCase converts the entire String to upper or lowercase
5
Examples… String coin = “Nickel”; Demo… coderesult coin.length( )6 (even though the last letter is at index #5) coin.toUpperCase( )NICKEL coin.charAt(1)i coin.indexOf(“k”)3 coin.indexOf(“g”)–1 (since g was not found) coin.substring(1,3)“ic” (notice that letters 1 and 2 are the result, but not 3) coin.substring(2)“ckel” (starts at 2 and goes to the end)
6
Assignments 1.(Rush) Prompt the user to enter a word. If the word is “Neil,” then ask the user to enter a letter. If the letter is a or b, display “Peart.” If it’s another letter, display “Xanadu.” If the word is “Geddy,” then ask the user to enter a number. If the number is odd and divisible by 9, display “Lee.” If the number is negative or equals 2112, display “YYZ.” If the word is something else, display the 3 rd letter of the word. Display how many letters are in the word. Do you like the band Rush? You should. (see next slide)
7
2. (Middle) Prompt the user to enter their middle name. If the 1 st letter is a vowel, display “Vowel”, otherwise, “consonant” If there are more than 8 letters in the name, display “long”, if between 5 and 8 (inclusive), display “medium”, otherwise, display “short.” Display whether the # of letters in the name is even or odd. Also, if it’s even, display whether or not it is divisible by 4. If it’s odd, display whether or not it is divisible by 5. If the letter ‘a’ is in the name, display the first index # it was found at (example: “The letter a was first found at index #3”) If it is not found, then say “The letter a was not found.” If the first and last letters in the name are the same, display “same,” otherwise “different” If the 2 nd letter and the 2 nd -to-last letter are both k, display “YES K” Display the 1 st through the 3 rd letters. Display all letters from the 2 nd to the end of the name.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.