Java basics variables

1,254 views
990 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,254
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
55
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java basics variables

  1. 1. JAVA BASICS VARIABLES
  2. 2. Objectives1. Understand how to use variables2. Recognise the different datatypes1. Use a range of arithmetic operators2. Learn how to call a subroutine3. Learn how to round a number4. Become more confident writing your original JAVA programs
  3. 3. Using Variables 1. What will the output be for this code? The answer is: 17public class AddingExample{ Before you can use them in your code public static void main(String[] args) you must declare your variables. This { includes writing two things: int num1; // declaring the variables int num2; The DATATYPE of the variable (e.g int) int result; The IDENTIFIER (name) of the variable num1 = 10; // assigning values to the variables Once declared you num2 = 7; can then just use the result = num1 + num2; identifier to change the value or use it in System.out.println(“The answer is: ” + result); calculation etc }}
  4. 4. Using variablesWhen you declare a variable the computer: MEMORY • Allocates some memory large enough to hold the data num1 • Assigns that memory block the identifier you entered result num2When you assign a value with the = sign MEMORY • The value is stored in the memory location for that variable num1 10 result 17 num2 7
  5. 5. Datatypes There are several datatypes you can use Data type Description Example Storage required Stores a collection of characters Varies dependingString “Computer” (numbers, text etc.) on no. of chars A single character (text, number,char „6‟, „F‟, etc 1 byte symbol etc)int A whole number 5 2 bytesdouble A decimal number 2.5 4 bytesboolean Stores either TRUE or FALSE TRUE 1 byte String greeting = “Hello"; The first two lines of code to the left declare a variable AND then assign a value to them. boolean passed = false; The last example would have a value double percentageScore; assigned later in the program
  6. 6. Creating text variables1. What is the name of the class? public class StringVariables StringVariables {2. What are the names of the public static void main(String[] args)variables? { greeting, name String greeting = “Hello"; System.out.println( greeting );3. What is the data type for thesevariables String name = “Computing Students"; String (which means text) System.out.println( name ); }4. What will the output be for this }code? Hello When this program is run: Computing Students • A variable called „greeting‟ is made and the value Hello is assigned to it on the same line. • The value assigned to greeting is printed out • This is repeated for a new variable called „name‟
  7. 7. More complex text variables 1. What will the output be for this code? Hi, my name is Billy public class MoreMessages When used like this { the ‘=‘ is called the assignment operator public static void main(String[] args) { String myName = “Billy"; System.out.println("Hi, my name is " + myName); } } The ‘+’ symbol is used to join pieces of text together
  8. 8. Numerical Variables 1. What will the output be for this code? The answer is: 17public class AddingExample{ public static void main(String[] args) { int num1; // declaring the variables int num2; int result; num1 = 10; // assigning values to the variables num2 = 7; result = num1 + num2; System.out.println(“The answer is: ” + result); }}
  9. 9. Ex 2.1 – Simple ArithmeticAim: Create a simple program that adds two numbersDescriptionWrite a program called Arithmetic1.java that calculates the sum3.5 + 3.75. HINT Think about the correct datatypes. Look at the previous slide for some guidance.Difficulty rating Skills: Use of variables, arithmetic & datatypes
  10. 10. Arithmetic Operations Operation Symbol Meaning ExamplesAddition + 13 + 2 = 15Subtraction - 13 – 5 = 8Multiplication * 6 * 6 = 36 13 / 5 = 2.6 The result can be a decimalOrdinary Division / 15 / 3 = 5.0 number 2 / 9 = 0.222 13 DIV 5 = 2 the result is just the integer partQuotient (division) DIV 15 DIV 3 = 5 of the actual answer 2 DIV 9 = 0 13 DIV 5 = 3Remainder The result is the remainder of the MOD 15 DIV 3 = 0(division) calculation 2 DIV 9 = 2
  11. 11. Ex 2.2 – Multiple ArithmeticAim: Create a program that performs a range of arithmetic calculationsDescriptionWrite a program called Arithmetic2.java that takes 2 variableswith the values 15 and 5 and produces the following output:Output HINT You will need 3 variables (that you can reuse for each calculation) 1. num1 2. num 3. answerDifficulty rating Skills: Use of variables &arithmetic statements
  12. 12. Ex 2.3 – Kelly Koms Telephone billAim: Create a program that works out a person’s phone billDescriptionWrite a program that breaks down and works out the total of a personsphone bill. It should show them the output below:Output Use the data: 352 texts at 8p 116 mins at 12p HINT It is best to have 3 separate variables for the different totals. Look at slide 7 for how to output Strings and variables (Use “n” to space out your output)Difficulty rating Skills: Use of arithmetic & concatenating Strings
  13. 13. Ex 2.4 – BMIAim: Create a program that works out a person’s BMIDescriptionWrite a program called SimpleBMI.java that works out the BMI ofsomeone who is 1.80m and 70kg.The calculation is: BMI = weight / ( height 2 )OutputOn the next slide we will look at how to round up the value to two decimal places. Don‟tworry about this until you have completed this programDifficulty rating Skills: Use of variables &arithmetic statements
  14. 14. Rounding Numbers This is where the subroutine „round‟ is being called by using it‟s name. In brackets it has the value to be rounded (bmi) and the number of decimal places to round it to (2)The code circled in green is a subroutine called ‘round’ that rounds a value to a set number ofdecimal places. This subroutine requires two pieces of data (parameters) before it will work. double d – The value to be rounded, int decimalPlace – the number of decimal places to round to. It will ‘return’ a value that has been rounded upRemember a subroutine won‟t do anything unless it is „called‟ inside the main subroutine. (line 13)
  15. 15. More on calling subroutines You can see the code to the left has 3 subroutines. Starting on lines 9, 18 & 27. There is only 1 line of code in the main subroutine. Calling a subroutine When we call a subroutine we use it‟s IDENTIFIER (it‟s name). As seen on line 29 When the code is executed it will go to the main method. When it gets to line 29 the computer will execute lines 9 – 16. As subtractingSubRoutine is NOT CALLED anywhere it (remember this is the will NOT GET EXECUTED. ONLY subroutine that automatically executes when the program is run).
  16. 16. Ex 2.5 – Exam markAim: Create a program that works out a students % score for different 3testsDescriptionWrite a program called Test.java that works out the % score of 3 differenttests.Output Use the data: Test 1: 10.5/20 Test 2: 17/20 Test 3: 77/98 HINT Do the first test and try to run and compile it. Then do the other tests. Notice there is something different between the first test score and the last 2.Difficulty rating Skills: Use of arithmetic, datatypes & rounding
  17. 17. Some important things to noteWhen writing code it is good to break up a larger programs intosmall subroutines as this makes it easier to write, debug andunderstand.(For now most of your programs are small enough be writtendirectly in the main subroutine).Before using variables you must declare them first. This involvessupplying the datatype and it‟s identifier.
  18. 18. Some important things to noteWhen working out an arithmetic calculation and storing as adouble at least one of the variables involved in the calculationhas to be stored as a double (even if it is in fact just an integer.int num1 = 3; This would NOT work asInt num2 = 5; neither of the variables beingdouble result = num1 / num2 divided are stored as doublesint num1 = 3; This would work as num2 is stored as a double (evendouble num2 = 5; though 5 is an integer we stilldouble result = num1 / num2 have to store it as a double)

×