DOC PREVIEW
USF CS 110 - Lecture Notes

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Java IntroStrings•“This is a string.”–Enclosed in double quotes•“This is “ + “another “ + “string”–+ concatenates strings•“This is “ + 1 + “ string”–+ can also concatenate numbers and stringsEscape Sequences•Same as in python–\t tab–\n newline–\” double quote–\\ backslashVariables•Unlike python, Java is strongly typed–You MUST specify the type of each variable•Numeric primitive types–int,long, float, doubleExamplesint age; double cost;int x = 5;double a = 9, b = 9.5;•x=b; //COMPILATION ERROR•age = 10.6; //COMPILATION ERROROther Primitive Types•char–A single character enclosed in single quotes–char first_initial = 'S';•boolean–true/false–boolean isPair = true;Constants•final keyword prevents changing value of variable–final double TAX_RATE = .0825;Expressions•+, -, *, /, %•Same precedence as pythonExpressions•++ = increase by 1–int i = 0;–i++; //add one to i•-- = decrease by 1–int i = 10;–i--; //subtract 1 from i•pre/post increment–int total1 = ++i;–int total2 = i++;Data Conversion•Assignment conversion–assign value of one type to variable of another type–OK for widening conversions•int i = 5; double d = i;Data Conversion•Promotion–operators automatically convert operands–double a = 5.0;–int b = 6;–double result = a/b;Data Conversion•Casting–programmer explicitly specifies–specify type in parens–double d = 9; int x=(int)d;Keyboard Input•The Scanner classimport java.util.Scanner;Scanner scan = new Scanner (System.in);scan.nextLine()scan.nextInt()scan.nextDouble()import java.util.Scanner;public class Tax { public static void main(String[] args) {//A program to calculate tax and total cost for an item.//constant rate of taxationfinal double TAX_RATE = .0825;Scanner s = new Scanner(System.in);System.out.println("Enter item cost: ");//ask user for the cost of the itemdouble cost = s.nextDouble();//calculate the taxdouble tax = cost*TAX_RATE;//calculate total costdouble total = cost+tax;System.out.println("Cost: " + cost);System.out.println("Tax : " + tax);System.out.println("Total: " + total); }}Exercises1. Write a program that prompts the user for two integers and displays the sum, difference, product, and quotient of the


View Full Document

USF CS 110 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?