4 WORKING WITH DATA TYPES AND OPERATIONS

Size: px
Start display at page:

Download "4 WORKING WITH DATA TYPES AND OPERATIONS"

Transcription

1 WORKING WITH NUMERIC VALUES 27 4 WORKING WITH DATA TYPES AND OPERATIONS WORKING WITH NUMERIC VALUES This application will declare and display numeric values. To declare and display an integer value in an application: 1. Open the Eclipse IDE, if necessary. 2. Right-click at the helloworld project, create a new package named helloworld.variables. 3. Create a new class named DemoVariables in the helloworld.variables package. 4. Position the insertion point after the opening curly brace, press Enter, and then type the following main()method and its curly braces: public static void main(string[] args) { } 5. Position the insertion point after the opening curly brace in the main()method, press Enter and then type the following to declare a variable of type int named intno with a value of 296. int intno = 296; 6. Press Enter at the end of the intno declaration statement then type the following two statements. The first statement uses the print()method to output The int is and leaves the insertion point on the same output line. The second statement uses the println()method to output the value of intno and advances the insertion point to a new line. System.out.print( The int is ); System.out.println(intNo); 7. Save the file application. If necessary correct any errors and then save again. 8. Run the application. The output should appear as shown in Figure 4.1.

2 28 Java Programming Workbook Figure 4.1 Output of the DemoVariables application ADDING VARIABLES TO A CLASS To declare two or more variables in the application: 1. Return to the DemoVariables.java file in Eclipse. Rename the class DemoVariables2. 2. Position the insertion point at the end of the line that contains the intno declaration, press Enter and then type the following variable declarations on separate lines: short shortno = 19; long longno = L; 3. Position the insertion point at the end of the line that contains the println()method that displays the intno value, press Enter and then type the following statements to display the values of the two new variables: System.out.print( The short is ); System.out.println(shortNo); System.out.print( The long is ); System.out.println(longNo); 4. Save the application using the filename DemoVariables2.java. If necessary correct any errors and then save again. 5. Run the application. The output should appear as shown in Figure 4.2.

3 CONCATENATING STRING 29 Figure 4.2 Output of the DemoVariables2 application CONCATENATING STRING To change the two print methods into a single statement: 1. Open the DemoVariables2.java file and rename the class DemoVariables3. 2. Use the mouse to select the two statements that print The int is and the value of intno, and then press Delete to delete them. In place of the deleted statements, type the following println() statement: System.out.println( The int is + intno); 3. Select the two statements that produce output for the short variable, press Delete to delete them and then type the following statement: System.out.println( The short is + shortno); 4. Finally select the two statement that produce output for the long variable, delete them and then type the following statement: System.out.println( The long is + longno); 5. Save the file as DemoVariables3.java then run the application.

4 30 Java Programming Workbook Figure 4.3 Output of the DemoVariables3 application INTEGER ARITHMETIC OPERATORS You use arithmetic operators to perform calculations with values in your programs. A value used on either side of an operator is an operand. In the expression , 20 and 9 are operands. Operator Description Example + Addition , the result is 29 Subtraction 20 9, the result is 11 * Multiplication 20 * 9, the result is 180 / Division 20 / 9, the result is 2 (not 2.22) % Modulus 20 % 9, the result is 2 (that is, 20 / 9 with a remainder of 2) (remainder)

5 USING ARITHMETIC STATEMENTS 31 USING ARITHMETIC STATEMENTS To use some arithmetic statements in an application: 1. Open the DemoVariables3.java file in Eclipse and change the class to DemoVariables4. 2. Position the insertion point at the end of the last line of the current variable declarations, press Enter and then type the following declarations: int value1 = 43, value2 = 10, sum, difference, product, quotient, modulus; 3. Position the insertion point after the statement that prints the longno variable, press Enter and then type the following statements on separate lines: sum = value1 + value2; difference = value1 value2; product = value1 * value2; quotient = value1 / value2; modulus = value1 % value2; 4. After the calculations you just added, type the following output statements: System.out.println( Sum is + sum); System.out.println( Difference is + difference); System.out.println( Product is + product); System.out.println( Quotient is + quotient); System.out.println( Modulus is + modulus); 5. Save the application as DemoVariables4.java. Run the application. Your output should look like Figure 4.4. Analyze the output and confirm that the arithmetic is correct. USING FLOATING-POINT VARIABLES To add floating-point variables to the application: 1. Open the DemoVariables4.java file in Eclipse and change the class to DemoVariables5. 2. Position the insertion point after the line that declares the integer variables, press Enter and then type the following to add some new floating-point variables to the class: double doubnum1 = 2.3, doubnum2 = 14.8, doubresult; 3. Press Enter and then type the following statements to perform arithmetic and produce output: doubresult = doubnum1 + doubnum2; System.out.println( The sum of the doubles is + doubresult); doubresult = doubnum1 * doubnum2; System.out.println( The product of the doubles is + doubresult);

6 32 Java Programming Workbook 4. Save the file as DemoVariables5.java, compile it and then run the program. The output is shown in Figure 4.5. Figure 4.4 Output of the DemoVariables4 application Figure 4.5 Output of the DemoVariables5 application

7 USING BOOLEAN VARIABLES 33 COMPARISON OPERATORS A comparison operator compares two items. An expression containing a comparison operator has a Boolean value. Operator Description True example False example < Less than 5 < < 5 > Greater than 10 > 5 5 > 10 == Equal to 10 == 10 5 == 10 <= Less than or equal to 5 <= 5 10 <= 5 >= Greater than or equal to 10 >= 5 5 >= 10!= Not equal to 5!= 10 5!= 5 USING BOOLEAN VARIABLES To add Boolean variables to an application: 1. Open the DemoVariables5.java file in Eclipse and change the class name to DemoVariables6. 2. Position the insertion point at the end of the line with the double variable declarations, press Enter and then type the following on one line to add two new Boolean variables to the application: boolean isprogrammingfun = (10 > 5), isprogramminghard = (10 <= 5); 3. Next, add some print statement to display the values. Press Enter and then type the following statements: System.out.println( The value of isprogrammingfun is + isprogrammingfun); System.out.println( The value of isprogramminghard is + isprogramminghard); 4. Save the file as DemoVariables6.java and then run the application. The output as in Figure 4.6.

8 34 Java Programming Workbook Figure 4.6 Output of the DemoVariables6 application OPERATOR PRECEDENCE The Java compiler follows a set of rules called operator precedence to determine the order in which the operations should be performed. The order is the same as in ordinary mathematics. Operator Hierarchy Associativity Operation ( ) Left to right parentheses for explicit grouping * / % Left to right multiplication, division, modulus + Left to right addition, subtraction USING OPERATOR PRECEDENCE To develop a better understanding of the rules of operator precedence, consider the evaluation of a second-degree polynomial ( y = ax 2 + bx +c ). To implement operator precedence in the above application: 1. Open the DemoVariables6.java file in Eclipse and change the class name to DemoVariables7.

9 USING INCREMENT AND DECREMENT OPERATORS Position the insertion point at the end of the line with the Boolean variable declarations, press Enter and then type the following on one line to add a new integer variable to the application: int poly = (2 * 5 * 5) + (3 * 5) + 7; 3. Next, add some print statement to display the values. Press Enter and then type the following statements: System.out.println( The value of the second-degree polynomial is + poly); 4. Save the file as DemoVariables7.java and then run the application. The output as in Figure 4.7. Figure 4.7 Output of the DemoVariables7 application USING INCREMENT AND DECREMENT OPERATORS To implement increment and decrement operators in an application: 1. Open the DemoVariables7.java file in Eclipse and change the class name to DemoVariables8. 2. Position the insertion point at the end of the line with the integer variable declarations for polynomial example before, press Enter and then type the following on one line to add four new integer variables to the application: int num1 = 1, num2 = 2, num3, num4; 3. Then press Enter and then type the following statements on separate lines: num3 = ++num2; num4 = num1++; num3++; 4. After the calculations you just added, type the following output statements:

10 36 Java Programming Workbook System.out.println( The value of num1 is + num1); System.out.println( The value of num2 is + num2); System.out.println( The value of num3 is + num3); System.out.println( The value of num4 is + num4); 5. Save the file as DemoVariables8.java and then run the application. The output as in Figure 4.8. Figure 4.8 Output of the DemoVariables8 application USING NUMERIC TYPE CONVERSION When you are performing arithmetic involving two operands of different types, Java will automatically (or implicitly) converts the operand based on the following order: 1. double 2. float 3. long 4. int To use numeric type conversion in an application: 1. Create a new class named NumericConversion in the helloworld.variables package. 2. Position the insertion point after the opening curly brace, press Enter, and then type the following main()method and its curly braces: public static void main(string[] args) { }

11 USING NUMERIC TYPE CONVERSION 37 INCREMENT AND DECREMENT OPERATORS In computer programming it is quite common to want to increase or decrease the value of an integer type by 1. Java provides the increment and decrement operators that add 1 to a variable and subtract 1 from a variable, respectively. Operator Name Description ++i Pre-increment Add 1 to i The value is returned after the increment is made i = 1; j = ++i; Then j will hold 2 and i will hold 2. --i Pre-decrement Subtract 1 from i The value is returned after the decrement is made i = 1; j = --i; Then j will hold 0 and i will hold 0. i++ Post-increment Add 1 to i The value is returned before the increment is made i = 1; j = i++; Then j will hold 1 and i will hold 2. i-- Post-decrement Subtract 1 from i The value is returned before the decrement is made i = 1; j = i--; Then j will hold 1 and i will hold Position the insertion point after the opening curly brace in the main()method, press Enter and then type the following statement: int hoursworked = 37; double payrate = 4.50; double grosspay = hoursworked * payrate; System.out.println("Gross pay is "+ grosspay);

12 38 Java Programming Workbook 4. Save the application as NumericConversion.java. Run the application. Your output should look like Figure 4.9. Analyze the program and the output. Then confirm that the conversion is according to the order. 5. Now change the type of variable grosspay from double to int. What is the output? Figure 4.9 Output of the NumericConversion application You can purposely (or explicitly) override the automatic conversion imposed by Java by performing type cast. Type casting forces a value of one data type to be used as a value of another type. To use type casting in an application: 1. Open the NumericConversion.java file in Eclipse and change the class name to NumericConversion2. 2. Position the insertion point at the beginning of the line with the grosspay variable declaration. Change the declaration to the following statement: int grosspay = (int)(hoursworked * payrate); 3. Save the application as NumericConversion2.java. Run the application. Your output should look like Figure USING CHARACTER VARIABLES To use a character variable in an application: 1. Open the DemoVariables8.java file in Eclipse and change the class name to DemoVariables9.

13 USING CHARACTER VARIABLES 39 Figure 4.10 Output of the NumericConversion2 application 2. Position the insertion point after the line that declares the integer variables for increment and decrement example before, press Enter and then type the following statements. The first declares two character grades; the second display them. char mygrade = A, myfriendsgrade = C ; System.out.println( Our grades are + mygrade + and + myfriendsgrade); 3. Save the file as DemoVariables9.java and then test the application. Your output should look like Figure Figure 4.11 Output of the DemoVariables9 application

14 40 Java Programming Workbook COMMON ESCAPE SEQUENCES Escape sequence Description \b Backspace; moves the cursor on space to the left \t Tab; moves the cursor to the next tab stop \n Newline or linefeed; moves the cursor to the beginning of the next line \r Carriage return; moves the cursor to the beginning of the current line \ Double quotation mark; prints a double quotation mark \ Single quotation mark; prints a single quotation mark \\ Backslash; prints a backslash character The escape sequence \n (newline), \ (double quote) and \\ (backslash) operate as expected in the dialog boxes. Others do not work in the GUI environment. USING ESCAPE SEQUENCES Next you will add statements to your DemoVariables9.java file to use the \n and \t escape sequences. To use the escape sequences in an application: 1. Open the DemoVariables9.java file in Eclipse and change the class name to DemoVariables Position the insertion point after the statement that prints the values of mygrade and myfriendsgrade variables. Press Enter to start a new line, and then type the following statements: System.out.println( \nthis is on one line\nthis is on another ); System.out.println( This shows\thow\ttabs\twork ); 3. Save the file as DemoVariables10.java and then test the application. Your output should look like Figure 4.12.

15 Do it yourself 41 Figure 4.12 Output of the DemoVariables10 application Do it yourself 4.1 Based on the algorithm, write a Java program to calculate the area of a circle. Assign an integer values to the variable radius for example, int radius = 2. Save the class as Circle.java. Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x Display the area 4.2 Write a Java program that declares variables to represent the length and width of a room. Assign appropriate values to the variables for example, length = 20 and width = 15. Then compute and display the floor space in square feet. Display explanatory text with the value for example, The floor space is 300 square feet. Save the class as Room.java. [Note: Floor space = length * width.] 4.3 Write a program that converts pounds into kilograms. Assign appropriate values to the variable in pounds, converts it to kilograms, and displays the result. Save the class as Weight.java. [Note: One pound is kilograms.]

16 42 Java Programming Workbook 4.4 Write a class that displays the following heading on one line: First Name Last Name Matric Number Phone Number Display your first name, last name, matric number and phone number on the second line, below the appropriate column headings. Save the class as Escape.java. 4.5 Write a program that prints a box and a diamond as follows. Save the class as Box.java. ********* * * * * * * * * * * * * * * * * * * * * * ********* * 4.6 Using the escape sequences you learned before, write a program that calculates the squares and cubes of the numbers from 0 to 3 and uses tabs to print the following table of values. Save the class as Number.java. number square cube Identify and fix the error in the following code: public class Salary { public static void main(string[] args) { int paydate; System.out.println( Salary will be pay on + paydate + January ); }

17 CORRECTING ERRORS 43 CORRECTING ERRORS You might make typing errors as you enter Java statements into your text editor or Eclipse IDE. When you issue the command to compile the class containing errors, the Java compiler produces one or more error messages. Basically there are three types of errors that you will contend with when writing programs: Syntax errors (or semantic errors) Runtime errors Logic errors The errors become more difficult to find and fix as you move down the above list. SYNTAX ERRORS Syntax errors represent grammar errors in the use of the programming language. Common examples are: Misspelled variable and function names Missing semicolons Improperly matches parentheses, square brackets and curly braces Incorrect format in selection and loop statements To show syntax error in a program: 1. Create a new package named helloworld.errors under the helloworld project in Eclipse IDE. 2. Right-click at the helloworld.errors package and create a new class named ShowSyntaxErrors. 3. Position the insertion point after the opening curly brace, press Enter, and then type the following: public class ShowSyntaxErrors { public static void main(string[] args) { i = 30; System.out.println(i + 4); } } 4. Save the application and then test the application. The Errors in Workspace window will appear. Click Proceed. The output should look like the following Figure Rename the class name to ShowSyntaxErrorsFix. 6. To correct the errors, position the insertion point before the statement that declare the variable i to 30. Then type int and insert a space. 7. Save the application as ShowSyntaxErrorsFix.java and then test the application. Your output should look like Figure 4.14.

18 44 Java Programming Workbook Figure 4.13 Output of the ShowSyntaxErrors application Figure 4.14 Output of the ShowSyntaxErrorsFix application 8. You can now delete the ShowSyntaxErrors.java file. RUNTIME ERRORS Runtime errors occur when a program with no syntax errors asks the computer to do something that the computer is unable to reliably do. Common examples are: Trying to divide by a variable that contains a value of zero Trying to open a file that doesn't exist There is no way for the compiler to know about these kinds of errors when the program is compiled. To show runtime error in a program: 1. Right-click at the helloworld.errors package and create a new class named ShowRuntimeErrors. 2. Position the insertion point after the opening curly brace, press Enter, and then type the following:

19 CORRECTING ERRORS 45 public class ShowRuntimeErrors { public static void main(string[] args) { int i = 1 / 0; System.out.println(i); } } 3. Save the application and then test the application. The output should look like the following Figure Figure 4.15 Output of the ShowRuntimeErrors application LOGIC ERRORS Logic errors occur when there is a design flaw in your program. Common examples are: Multiplying when you should be dividing Adding when you should be subtracting Opening and using data from the wrong file Displaying the wrong message To show logic error in a program: 1. Right-click at the helloworld.errors package and create a new class named ShowLogicErrors. 2. Position the insertion point after the opening curly brace, press Enter, and then type the following: public class ShowLogicErrors { //Determine if a number is between 1 and 100 inclusively public static void main(string[] args) { int i = 100; System.out.println("The number is between 1 and 100, " + "inclusively? " + ((1 < i) && (i < 100))); } }

20 46 Java Programming Workbook 3. Save the application and then test the application. The output should look like the following Figure Analyze the program and the output. Figure 4.16 Output of the ShowLogicErrors application GETTING USER INPUT USING SCANNER CLASS To get input from a user using Scanner class in an application: 1. In the helloworld project, create a new class named ScannerInputs in the helloworld package. 2. Type the import statement after the package declaration line which allows you to use the Scanner class: import java.util.scanner; 3. Position the insertion point after the opening curly brace, press Enter, and then type the following main()method and its curly braces: public static void main(string[] args) { } 4. Position the insertion point after the opening curly brace in the main()method, press Enter and then type the following: Scanner scan = new Scanner(System.in); System.out.println( Enter your name ); String name = scan.nextline(); System.out.println( Enter first integer ); int first = scan.nextint(); System.out.println( Enter second integer ); int second = scan.nextint();

21 GETTING USER INPUT USING INPUT DIALOGS 47 System.out.print(name +, first integer is + first); System.out.println( and second integer is + second); 5. Save the file as ScannerInputs.java, compile and then test the application. 6. Highlight the console area and type in your name and press Enter. Do the same for the first and second integer. Your output should look like Figure Figure 4.17 Output of the ScannerInputs application GETTING USER INPUT USING INPUT DIALOGS To get input from a user using an input dialog box: 1. Create a new class named DialogInputs in the helloworld package. 2. Type the import statement that allows you to use the JOptionPane class: import javax.swing.joptionpane; 3. Position the insertion point after the opening curly brace, press Enter, and then type the following main()method and its curly braces: public static void main(string[] args) { }

22 48 Java Programming Workbook 4. Position the insertion point after the opening curly brace in the main()method, press Enter and then type the following: String first = JOptionPane.showInputDialog(null, Enter first integer, Example 1, JOptionPane.QUESTION_MESSAGE); JOptionPane.showMessageDialog(null, First integer is + first, Example 1, JOptionPane.INFORMATION_MESSAGE); String second = JOptionPane.showInputDialog(null, Enter second integer, Example 2, JOptionPane.QUESTION_MESSAGE); JOptionPane.showMessageDialog(null, Second integer is + second, Example 2, JOptionPane.INFORMATION_MESSAGE); 5. Save the application and then test the application. Your outputs should look like Figure Figure 4.18 Outputs of the DialogInputs application CONVERTING STRINGS TO INTEGERS AND DOUBLES (WHEN USING INPUT DIALOGS) The input returned from the input dialog box is a string. To convert a string to an integer or double value, use the following: int intvalue = Integer.parseInt( ); double doublevalue = Double.parseDouble( ); To convert a String to an integer in a dialog box: 1. Open the DialogInputs.java in Eclipse and change the class name to DialogInputs2.

23 FORMATTED OUTPUT Position the insertion point after the statement that display the second integer in a dialog box, press Enter and then type the following: int firstint = Integer.parseInt(first); int secondint = Integer.parseInt(second); JOptionPane.showMessageDialog(null, "Sum of the two integers is "+ (firstint + secondint), "Example 3", JOptionPane.INFORMATION_MESSAGE); 3. Save the application as DialogInput2.java and then test the application. Your output should look like Figure Figure 4.19 Outputs of the DialogInputs2 application FORMATTED OUTPUT You can format the output of a program using the printf method. This method allows you to format numeric values in two useful ways: By specifying the number of decimal places to display. By specifying the field size in which to display values. The syntax to invoke this method is System.out.printf(format, items);

24 50 Java Programming Workbook To specify a number of decimal places to display in an application: 1. Create a new class named FormatOutput in the helloworld package. 2. Position the insertion point after the opening curly brace, press Enter, and then type the following main()method and its curly braces: public static void main(string[] args) { } 3. Position the insertion point after the opening curly brace in the main()method, press Enter and then type the following: int age = 23; double money = 123; System.out.println( Age is + age); System.out.println( Money is RM + money); System.out.println( After format: ); System.out.printf("Age is %d and money is RM%.2f, age, money); 4. Save and then test the application. Your output should look like Figure Figure 4.20 Output of the FormatOutput application

25 FORMATTED OUTPUT 51 To specify a field size with printf() in an application: 1. Create a new class named FormatOutput2 in the helloworld package. 2. Position the insertion point after the opening curly brace, press Enter, and then type the following main()method and its curly braces: public static void main(string[] args) { } 3. Position the insertion point after the opening curly brace in the main()method, press Enter and then type the following: int no1 = 1, no2 = 23, no3 = 456, no4 = 7890; System.out.printf( %5d\n, no1); System.out.printf( %5d\n, no2); System.out.printf( %5d\n, no3); System.out.printf( %5d\n, no4); 4. Save and then test the application. Your output should look like Figure Figure 4.21 Output of the FormatOutput2 application

26 52 Java Programming Workbook Do it yourself 4.8 Write a Java program that reads in two integers and display the sum of the two integers. Save the class as Sum.java. 4.9 Write a Java program that asks the user to enter two numbers using an input dialog and prints the sum, product, difference and quotient of the two numbers in a message dialog. Save the class as InputNumber.java Write a Java program that reads a Fahrenheit degree in double from an input dialog box, then converts it to Celsius and displays the result in a message dialog box. Save the class as Temperature.java. The formula for the conversion is: [Note: In Java, 5 / 9 is 0, so you need to write 5.0 / 9 in the program to obtain the correct result.] Celsius = (5.0/ 9) * (Fahrenheit 32) 4.11 What does the following code print? System.out.printf( %s\n%s\n, Welcome to, Java programming! ); CASE PROJECT When the MakingMoneyATM application first execute, it displays a Welcome! greeting. After displaying the greetings, the company has asked you to improve the application. The application should then prompt the users to enter their account number. Then the ATM will display the user s account number. Assign an appropriate value for the account number. Rewrite, recompile and retest the MakingMoneyATM class. Be certain to use appropriate comments in your class.

Chapter 2 ELEMENTARY PROGRAMMING

Chapter 2 ELEMENTARY PROGRAMMING Chapter 2 ELEMENTARY PROGRAMMING Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk ١ Objectives To write Java programs to perform simple

More information

Chapter 2 Primitive Data Types and Operations. Objectives

Chapter 2 Primitive Data Types and Operations. Objectives Chapter 2 Primitive Data Types and Operations Prerequisites for Part I Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs,

More information

Chapter 2 Primitive Data Types and Operations

Chapter 2 Primitive Data Types and Operations Chapter 2 Primitive Data Types and Operations 2.1 Introduction You will be introduced to Java primitive data types and related subjects, such as variables constants, data types, operators, and expressions.

More information

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved. CHAPTER 2 1 2 1 // Fig. 2.1: Welcome1.java 2 // A first program in Java 3 4 public class Welcome1 { 5 public static void main( String args[] ) 6 { 7 System.out.println( "Welcome to Java Programming!" );

More information

JAVA Programming Concepts

JAVA Programming Concepts JAVA Programming Concepts M. G. Abbas Malik Assistant Professor Faculty of Computing and Information Technology University of Jeddah, Jeddah, KSA mgmalik@uj.edu.sa Programming is the art of Problem Solving

More information

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process Entry Point of Execution: the main Method Elementary Programming EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG For now, all your programming exercises will

More information

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time Tester vs. Controller Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG For effective illustrations, code examples will mostly be written in the form of a tester

More information

Elementary Programming

Elementary Programming Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Learn ingredients of elementary programming: data types [numbers, characters, strings] literal

More information

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Selection

More information

Chapter 2: Using Data

Chapter 2: Using Data Chapter 2: Using Data TRUE/FALSE 1. A variable can hold more than one value at a time. F PTS: 1 REF: 52 2. The legal integer values are -2 31 through 2 31-1. These are the highest and lowest values that

More information

Chapter 2: Using Data

Chapter 2: Using Data Chapter 2: Using Data TRUE/FALSE 1. A variable can hold more than one value at a time. F PTS: 1 REF: 52 2. The legal integer values are -2 31 through 2 31-1. These are the highest and lowest values that

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1: Introduction Lecture Contents 2 Course info Why programming?? Why Java?? Write once, run anywhere!! Java basics Input/output Variables

More information

Chapter 02: Using Data

Chapter 02: Using Data True / False 1. A variable can hold more than one value at a time. ANSWER: False REFERENCES: 54 2. The int data type is the most commonly used integer type. ANSWER: True REFERENCES: 64 3. Multiplication,

More information

Chapter 2 Primitive Data Types and Operations

Chapter 2 Primitive Data Types and Operations Chapter 2 Primitive Data Types and Operations 2.1 Introduction You will be introduced to Java primitive data types and related subjects, such as variables constants, data types, operators, and expressions.

More information

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #2

More information

Simple Java Reference

Simple Java Reference Simple Java Reference This document provides a reference to all the Java syntax used in the Computational Methods course. 1 Compiling and running... 2 2 The main() method... 3 3 Primitive variable types...

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical problems

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M tutor Isam M. Al Jawarneh, PhD student isam.aljawarneh3@unibo.it Mobile Middleware

More information

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2 Java Foundations Introduction to Program Design and Data Structures 4th Edition Lewis TEST BANK Full download at : https://testbankreal.com/download/java-foundations-introduction-toprogram-design-and-data-structures-4th-edition-lewis-test-bank/

More information

Course Outline. Introduction to java

Course Outline. Introduction to java Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University January 15, 2015 Chapter 2: Data and Expressions CS 121 1 / 1 Chapter 2 Part 1: Data

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University April 21, 2015 Chapter 2: Data and Expressions CS 121 1 / 53 Chapter 2 Part 1: Data Types

More information

Visual C# Instructor s Manual Table of Contents

Visual C# Instructor s Manual Table of Contents Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 05 / 31 / 2017 Instructor: Michael Eckmann Today s Topics Questions / Comments? recap and some more details about variables, and if / else statements do lab work

More information

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

More information

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2 CONTENTS: Compilation Data and Expressions COMP 202 More on Chapter 2 Programming Language Levels There are many programming language levels: machine language assembly language high-level language Java,

More information

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value Lecture Notes 1. Comments a. /* */ b. // 2. Program Structures a. public class ComputeArea { public static void main(string[ ] args) { // input radius // compute area algorithm // output area Actions to

More information

Introduction to Computer Programming

Introduction to Computer Programming Introduction to Computer Programming Lecture 2- Primitive Data and Stepwise Refinement Data Types Type - A category or set of data values. Constrains the operations that can be performed on data Many languages

More information

Motivations 9/14/2010. Introducing Programming with an Example. Chapter 2 Elementary Programming. Objectives

Motivations 9/14/2010. Introducing Programming with an Example. Chapter 2 Elementary Programming. Objectives Chapter 2 Elementary Programming Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical problems

More information

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled Interpreted vs Compiled Python 1 Java Interpreted Easy to run and test Quicker prototyping Program runs slower Compiled Execution time faster Virtual Machine compiled code portable Java Compile > javac

More information

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on: Data and Expressions Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration And Use Of Variables Expressions

More information

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes Entry Point of Execution: the main Method Elementary Programming EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG For now, all your programming exercises will be defined within the

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

4 Programming Fundamentals. Introduction to Programming 1 1

4 Programming Fundamentals. Introduction to Programming 1 1 4 Programming Fundamentals Introduction to Programming 1 1 Objectives At the end of the lesson, the student should be able to: Identify the basic parts of a Java program Differentiate among Java literals,

More information

Chapter 2: Using Data

Chapter 2: Using Data Chapter 2: Using Data Declaring Variables Constant Cannot be changed after a program is compiled Variable A named location in computer memory that can hold different values at different points in time

More information

Chapter 3 Selection Statements

Chapter 3 Selection Statements Chapter 3 Selection Statements 3.1 Introduction Java provides selection statements that let you choose actions with two or more alternative courses. Selection statements use conditions. Conditions are

More information

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public

More information

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS) GETTING STARTED: YOUR FIRST JAVA APPLICATION 15 3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS) GETTING STARTED: YOUR FIRST JAVA APPLICATION Checklist: The most recent version of Java SE Development

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

More information

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators 1 Professor: Sana Odeh odeh@courant.nyu.edu Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators Review What s wrong with this line of code? print( He said Hello ) What s wrong with

More information

Chapter. Let's explore some other fundamental programming concepts

Chapter. Let's explore some other fundamental programming concepts Data and Expressions 2 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Data and Expressions Let's explore some

More information

COMP 202 Java in one week

COMP 202 Java in one week CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator COMP 202 Java in one week The Java Programming Language A programming language

More information

Full file at

Full file at MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) 25 % 1 is. 1) A) 2 B) 1 C) 0 D) 4 E) 3 2) Which of the following expressions will yield 0.5? (choose

More information

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Topics 1. Expressions 2. Operator precedence 3. Shorthand operators 4. Data/Type Conversion 1/15/19 CSE 1321 MODULE 2 2 Expressions

More information

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors Outline Overview history and advantage how to: program, compile and execute 8 data types 3 types of errors Control statements Selection and repetition statements Classes and methods methods... 2 Oak A

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

Basics of Java Programming

Basics of Java Programming Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Primitive Data, Variables, and Expressions; Simple Conditional Execution Unit 2, Part 1 Primitive Data, Variables, and Expressions; Simple Conditional Execution Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Overview of the Programming Process Analysis/Specification

More information

BASIC ELEMENTS OF A COMPUTER PROGRAM

BASIC ELEMENTS OF A COMPUTER PROGRAM BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators

More information

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Lesson 02 Data Types and Statements MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Topics Covered Statements Variables Constants Data Types

More information

Section 2.2 Your First Program in Java: Printing a Line of Text

Section 2.2 Your First Program in Java: Printing a Line of Text Chapter 2 Introduction to Java Applications Section 2.2 Your First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler are denoted using a. Two

More information

Chapter 3. Selections

Chapter 3. Selections Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of

More information

Introduction to Java Applications

Introduction to Java Applications 2 Introduction to Java Applications OBJECTIVES In this chapter you will learn: To write simple Java applications. To use input and output statements. Java s primitive types. Basic memory concepts. To use

More information

Chapter 2. Elementary Programming

Chapter 2. Elementary Programming Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,

More information

ECE 122 Engineering Problem Solving with Java

ECE 122 Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction Outline Problem: How do I input data and use it in complicated expressions Creating complicated expressions

More information

COMP 202. Java in one week

COMP 202. Java in one week COMP 202 CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator Java in one week The Java Programming Language A programming language

More information

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 Announcements Midterm Exams on 4 th of June (12:35 14:35) Room allocation will be announced soon

More information

Introduction to Java Applications; Input/Output and Operators

Introduction to Java Applications; Input/Output and Operators www.thestudycampus.com Introduction to Java Applications; Input/Output and Operators 2.1 Introduction 2.2 Your First Program in Java: Printing a Line of Text 2.3 Modifying Your First Java Program 2.4 Displaying

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University August 21, 2017 Chapter 2: Data and Expressions CS 121 1 / 51 Chapter 1 Terminology Review

More information

CEN 414 Java Programming

CEN 414 Java Programming CEN 414 Java Programming Instructor: H. Esin ÜNAL SPRING 2017 Slides are modified from original slides of Y. Daniel Liang WEEK 2 ELEMENTARY PROGRAMMING 2 Computing the Area of a Circle public class ComputeArea

More information

First Java Program - Output to the Screen

First Java Program - Output to the Screen First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the

More information

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent Programming 2 Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent information Input can receive information

More information

Section 2.2 Your First Program in Java: Printing a Line of Text

Section 2.2 Your First Program in Java: Printing a Line of Text Chapter 2 Introduction to Java Applications Section 2.2 Your First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler are denoted using a. Two

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

Getting started with Java

Getting started with Java Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

More information

Mr. Monroe s Guide to Mastering Java Syntax

Mr. Monroe s Guide to Mastering Java Syntax Mr. Monroe s Guide to Mastering Java Syntax Getting Started with Java 1. Download and install the official JDK (Java Development Kit). 2. Download an IDE (Integrated Development Environment), like BlueJ.

More information

Date: Dr. Essam Halim

Date: Dr. Essam Halim Assignment (1) Date: 11-2-2013 Dr. Essam Halim Part 1: Chapter 2 Elementary Programming 1 Suppose a Scanner object is created as follows: Scanner input = new Scanner(System.in); What method do you use

More information

Full file at

Full file at Chapter 2 Introduction to Java Applications Section 2.1 Introduction ( none ) Section 2.2 First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler

More information

Data Conversion & Scanner Class

Data Conversion & Scanner Class Data Conversion & Scanner Class Quick review of last lecture August 29, 2007 ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev Numeric Primitive Data Storing

More information

Zheng-Liang Lu Java Programming 45 / 79

Zheng-Liang Lu Java Programming 45 / 79 1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 45 / 79 Example Given a radius

More information

Introduction to Java Applications

Introduction to Java Applications 2 What s in a name? that which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would be the most fun? Peggy Walker Take some more

More information

download instant at

download instant at 2 Introduction to Java Applications: Solutions What s in a name? That which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would

More information

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Building Java Programs. Chapter 2: Primitive Data and Definite Loops Building Java Programs Chapter 2: Primitive Data and Definite Loops Copyright 2008 2006 by Pearson Education 1 Lecture outline data concepts Primitive types: int, double, char (for now) Expressions: operators,

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Java application A computer program that executes when you use the java command to launch the Java Virtual Machine

More information

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 123 Computer Creativity Introduction to Java Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) Introduce Java, a general-purpose programming language,

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics WIT COMP1000 Java Basics Java Origins Java was developed by James Gosling at Sun Microsystems in the early 1990s It was derived largely from the C++ programming language with several enhancements Java

More information

2: Basics of Java Programming

2: Basics of Java Programming 2: Basics of Java Programming CSC 1051 Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

Full file at

Full file at Java Programming, Fifth Edition 2-1 Chapter 2 Using Data within a Program At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional

More information

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Assignments Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Lecture 6 Complete for Lab 4, Project 1 Note: Slides 12 19 are summary slides for Chapter 2. They overview much of what we covered but are not complete.

More information

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs CSC 1051 Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

Programming for Engineers Introduction to C

Programming for Engineers Introduction to C Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

More information

Chapter 2: Basic Elements of Java

Chapter 2: Basic Elements of Java Chapter 2: Basic Elements of Java TRUE/FALSE 1. The pair of characters // is used for single line comments. ANS: T PTS: 1 REF: 29 2. The == characters are a special symbol in Java. ANS: T PTS: 1 REF: 30

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java basics: Compilation vs Interpretation Program structure Statements Values Variables Types Operators and Expressions

More information

Course PJL. Arithmetic Operations

Course PJL. Arithmetic Operations Outline Oman College of Management and Technology Course 503200 PJL Handout 5 Arithmetic Operations CS/MIS Department 1 // Fig. 2.9: Addition.java 2 // Addition program that displays the sum of two numbers.

More information

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Built in Libraries and objects CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Objects and Built in Libraries 1 Classes and Objects An object is an

More information

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein PRIMITIVE VARIABLES CS302 Introduction to Programming University of Wisconsin Madison Lecture 3 By Matthew Bernstein matthewb@cs.wisc.edu Variables A variable is a storage location in your computer Each

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

Java Basic Programming Constructs

Java Basic Programming Constructs Java Basic Programming Constructs /* * This is your first java program. */ class HelloWorld{ public static void main(string[] args){ System.out.println( Hello World! ); A Closer Look at HelloWorld 2 This

More information