Java Programming: Variables & Operators (PDF)

Document Details

IssueFreeRadon6091

Uploaded by IssueFreeRadon6091

Technological University Dublin

Nevan Bermingham

Tags

java programming java variables computer science

Summary

This presentation explains Java programming concepts, focusing on variables and operators. It provides examples of basic Java code and introduces the concept of classes.

Full Transcript

INTERNATIONAL & ACCESS FOUNDATION PROGRAMMES COMPUTER SCIENCE SEMESTER ONE Part 3 Java Programming Variables & Operators © Nevan Bermingham Main Principles of Java Coding In this section we are going to look at some of the main prin...

INTERNATIONAL & ACCESS FOUNDATION PROGRAMMES COMPUTER SCIENCE SEMESTER ONE Part 3 Java Programming Variables & Operators © Nevan Bermingham Main Principles of Java Coding In this section we are going to look at some of the main principles of Java, and how they are used: Variables Operators (, =, ==, !=, &&, ||,.....) Printing on screen and reading user input (generally referred to as input/output or I/O) First, lets look at a Program This is the classic “Hello World” Program in Java: class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. } } These are What does each part do? “Comment s” and are only for the ignores everything class HelloWorldApp from , and // public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. } } What does each part do? This is called a enough to know that every class HelloWorldApp { application public static void main(String[] args) begins with a { class definition. System.out.println("Hello World!"); // Display the string. } } What does each part do? In Java every application class HelloWorldApp must contain a { main method. public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. } } What does each part do? Uses the “System Class” the "Hello World!" class HelloWorldApp message to { standard output public static void main(String[] args) (normally the { Screen) System.out.println("Hello World!"); // Display the string. } } What does each part do? to bunch code together. We’ll class HelloWorldApp { be using these public static void main(String[] args) lot more later. { System.out.println("Hello World!"); // Display the string. } } What does each part do? ; All Java compiler the line has no class HelloWorldApp more code. { Leave it out by public static void main(String[] args) accident and { you’ll get an System.out.println("Hello World!"); // Display the string. error. } } Well...? Was that a little scary? Do not worry if you didn’t understand some or any of it, we will go over some basics first and then come back to the program. First, we’re going to cover off the basics of Variables and Operators – these are the first things you’ll need to understand before you can start coding. Variables Nearly all Applications (Programs) need to be able to store and manipulate data. For example:-  Numbers to be added or multiplied, and the answer.  A User Name  To count the occurrence of something Variables are places in the computer's memory where you store the data for a program. Each variable is given a unique name which you refer to it with, and can have many values => Hence the name Variable The opposite to a variable is a constant. Variables are like a box... “The Dublin Institute of Technology” Variable €34.56 Name 3.14159 42 265 Lets Look at an Example: To Multiply 2 numbers. First Number is stored in a variable x Second Number is stored in a variable called y The answer is stored in a variable called answer So:- answer = x MULTIPLIED BY y Note, this is Mathematically written: x x y = answer Or in computational mathematics: x * y = answer How would this program look? 1. Ask User to input first number. 2. Store this number in variable named x 3. Ask User to input second number. 4. Store this number in variable named y 5. Multiply x and y 6. Store this result in variable named answer 7. Print the value of answer to the screen Or in another way... Start Input First Number = x Input Second Number = y Output Answer = x * y Answer End Or in another way... Start In computing you have: Addition + Subtraction - Input First Number = x Multiplication * Division / These are called Input Second Number = y OPERATORS Output Answer = x * y Answer End Here’s the Java Program import java.util.Scanner; public class Multiply { // main method begins execution of Java application public static void main( String[] args ) { // create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); int x; // first number int y; // second number int answer; // to store the answer of x * y System.out.println( "Enter first integer: " ); // prompt x = input.nextInt(); // read first number from user System.out.println( "Enter second integer: " ); // prompt y = input.nextInt(); // read second number from user answer = x * y; // multiply numbers System.out.printf( "Sum is %d\n", answer ); // display sum to screen } // end method main } // end class Multiplication Here’s the Java Program import java.util.Scanner; public class Multiply { // main method begins execution of Java application public static void main( String[] args ) { // create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); int x; // first number int y; // second number Lets examine each part of int answer; // to store the answer of x * y System.out.println( "Enter first integer: " ); // prompt this Program.... x = input.nextInt(); // read first number from user System.out.println( "Enter second integer: " ); // prompt y = input.nextInt(); // read second number from user answer = x * y; // multiply numbers System.out.printf( "Sum is %d\n", answer ); // display sum to screen } // end method main } // end class Multiplication Comments public class Multiply { // main method begins execution of Java application public static void main(String[] args) { Comments are used to describe the Java code The compiler ignores everything from // to the end of the line Comments are good practice, and help you and others understand the flow of the code. Important: All your code should always contain comments. Operators answer = x * y; Operators perform a mathematical operation and return a result. Or, you could say another way:- They are used to manipulate data, data usually stored in variables. For a full list of Operators, see http://docs.oracle.com/javase/tutorial/jav a/nutsandbolts/opsummary.html Operators Assignment Operators = Arithmetic Operators - + * / % ++ -- Relational Operators > < >= < >= ) less than (=) less than or equal ( < >= y FALSE x >= y FALSE x

Use Quizgecko on...
Browser
Browser