Download presentation
Presentation is loading. Please wait.
Published byEmily Lindsey Modified over 5 years ago
1
First Programs CSE 1310 – Introduction to Computers and Programming
Vassilis Athitsos University of Texas at Arlington
2
Output System.out.println(…) prints out something.
System.out.println is the first piece of Java that we learn in this class. We will see in detail what kind of things can get printed. In the beginning, the things we care about printing are: Numbers. Strings (text).
3
Examples of System.out.println
In Netbeans, the program output always starts with "run:" and ends with "BUILD SUCCESSFUL …". Program: public class hello1 { public static void main(String[] args) { System.out.println("Have a nice day."); System.out.println( /7); } Output: run: Have a nice day. 7.3 BUILD SUCCESSFUL (total time: 0 seconds)
4
Examples of System.out.println
In Netbeans, the program output always starts with "run:" and ends with "BUILD SUCCESSFUL …". From now on, we will not be showing those lines. We will call "program output" what is between those lines. Program: public class hello1 { public static void main(String[] args) { System.out.println("Have a nice day."); System.out.println( /7); } Output: Have a nice day. 7.3
5
Syntax of System.out.println
Program: public class hello1 { public static void main(String[] args) { System.out.println("Have a nice day."); System.out.println( /7); } What you want to print is called the argument. To use System.out.println, you write a line like this: System.out.println(argument); In other words, you write System.out.println, followed by a left parenthesis, followed by an argument, followed by a right parenthesis, followed by a semicolon.
6
Syntax of System.out.println
Program: public class hello1 { public static void main(String[] args) { System.out.println("Have a nice day."); System.out.println( /7); } If the argument is text (also called a string), then it must be enclosed in double quotes. If the argument is a numerical expression, then System.out.println prints the result of that expression.
7
Syntax of System.out.println
Is each of these lines correct or not? If correct, what will it print? System.out.println("hello"); System.out.println(hello);
8
Syntax of System.out.println
Is each of these lines correct or not? If correct, what will it print? System.out.println("hello"); Correct, prints hello. System.out.println(hello); Incorrect, missing double quotes. Will not run.
9
Syntax of System.out.println
Is each of these lines correct or not? If correct, what will it print? System.out.println(" /7"); System.out.println( /7);
10
Syntax of System.out.println
Is each of these lines correct or not? If correct, what will it print? System.out.println(" /7"); Correct, prints /7 Note that the argument here is text. System.out.println( /7); Correct, prints 7.3 Note that the argument here is a numerical expression.
11
Syntax of System.out.println
Is each of these lines correct or not? If correct, what will it print? System.out.println("hello") System.out.println( /7)
12
Syntax of System.out.println
Is each of these lines correct or not? If correct, what will it print? System.out.println("hello") Incorrect. Missing semicolon at the end. Will not run. System.out.println( /7) Incorrect. Missing semicolon at the end. Will not run.
13
Syntax of System.out.println
Is each of these lines correct or not? If correct, what will it print? System.out.println "hello"; System.out.println /7;
14
Syntax of System.out.println
Is each of these lines correct or not? If correct, what will it print? System.out.println "hello"; Incorrect. Missing parentheses. Will not run. System.out.println /7; Incorrect. Missing parentheses. Will not run.
15
Syntax of System.out.println
Is each of these lines correct or not? If correct, what will it print? System.out.println "hello" (); System.out.println /7 ();
16
Syntax of System.out.println
Is each of these lines correct or not? If correct, what will it print? System.out.println "hello" (); Incorrect. Misplaced parentheses. Will not run. System.out.println /7 (); Incorrect. Misplaced parentheses. Will not run.
17
Syntax of System.out.println
As we saw a few slides ago, to use System.out.println, you write a line like this: System.out.println(argument); Java (like any programming language) is very strict. If you do not follow the syntax EXACTLY, it will refuse to execute that line. This is true not only for System.out.println, but for any syntax rules that we will see in this course.
18
Java as a Calculator. public class hello1 { public static void main(String[] args) { System.out.println((23*3) + 12/4.5); System.out.println( /7 - 4); } Output: 3.3 We can type in arbitrary numerical expressions, and Java evaluates them. This is still not that exciting. However, such calculations are a useful building block for real programs.
19
More Math Calculations
public class hello1 { public static void main(String[] args) { System.out.println(Math.pow(2, 10)); System.out.println(8 * Math.pow( /7, 4)); System.out.println(Math.sqrt(3)); System.out.println(4 - Math.sqrt(3+5/7.2)); } Output: 1024.0 312.5 Powers: becomes Math.pow(2, 10) becomes 8 * Math.pow( /7, 4) Roots 3 becomes Math.sqrt(3) 4− becomes 4 - Math.sqrt(3+5/7.2)
20
More Math Calculations
public class hello1 { public static void main(String[] args) { System.out.println(Math.PI); System.out.println(Math.sin(Math.PI / 2)); System.out.println(Math.cos(Math.PI / 2)); System.out.println(Math.tan(Math.PI / 2)); System.out.println(Math.log(12.5)); } } Output: 1.0 E-17 E16 The pi constant: Math.PI The sine of x: Math.sin(x) The cosine of x: Math.cos(x) The tangent of x: Math.tan(x) The natural logarithm of x: Math.log(x)
21
Division: Floating Point and Integer
public class hello1 { public static void main(String[] args) { System.out.println(7.0 / 4.0); System.out.println(7 / 4.0); System.out.println(7.0 / 4); System.out.println(7 / 4); System.out.println(7 % 4); } Output: 1.75 1 3 Floating point division: 7.0 / 4.0 7 / 4.0 7.0 / 4 They all evaluate to 1.75 Integer division: 7 / 4 evaluates to 1 7 % 4 produces the remainder of 7/4, so it evaluates to 3.
22
Circumference and Area of Circle
We want to write a program to compute the circumference and area of a circle. What do the the circumference and area of a circle depend on?
23
Circumference and Area of Circle
We want to write a program to compute the circumference and area of a circle. What do the the circumference and area of a circle depend on? The radius of the circule. circumference = 2 * pi * radius area = pi * radius2
24
Circumference and Area of Circle
Suppose we have a circle with radius = Computing the circumference of the circle: Circumference = 2 * pi * radius Code?
25
Circumference and Area of Circle
Suppose we have a circle with radius = Computing the circumference of the circle: Circumference = 2 * pi * radius System.out.println(2 * Math.PI * ); Output:
26
Circumference and Area of Circle
Suppose we have a circle with radius = Computing the circumference of the circle: Circumference = 2 * pi * radius System.out.println(2 * Math.PI * ); Output: Computing the area of the circle: area = pi * radius2 Code?
27
Circumference and Area of Circle
Suppose we have a circle with radius = Computing the circumference of the circle: Circumference = 2 * pi * radius System.out.println(2 * Math.PI * ); Output: Computing the area of the circle: area = pi * radius2 System.out.println(Math.PI * Math.pow(20.231, 2)); Output:
28
Circumference and Area of Circle
Suppose we have a circle with radius = Program: Is this a good program to sell to a user? public class hello1 { public static void main(String[] args) { System.out.println(2 * Math.PI * ); System.out.println(Math.PI * Math.pow(20.231, 2)); }
29
Circumference and Area of Circle
Suppose we have a circle with radius = Program: Is this a good program to sell to a user? No: the only way for the user to use this program is to modify the code every time, to specify the radius. That is bad. Users should not need to be programmers. public class hello1 { public static void main(String[] args) { System.out.println(2 * Math.PI * ); System.out.println(Math.PI * Math.pow(20.231, 2)); }
30
Circumference and Area of Circle
Suppose we have a circle with radius = Program: Any other issues/problems with this program? public class hello1 { public static void main(String[] args) { System.out.println(2 * Math.PI * ); System.out.println(Math.PI * Math.pow(20.231, 2)); }
31
Circumference and Area of Circle
Suppose we have a circle with radius = Program: Any other issues/problems with this program? The radius is specified TWICE. This is bad practice, introduces the risk of errors. Also, more painful to change the radius, we must change it in two places. public class hello1 { public static void main(String[] args) { System.out.println(2 * Math.PI * ); System.out.println(Math.PI * Math.pow(20.231, 2)); }
32
Circumference and Area of Circle
Suppose we have a circle with radius = Program: Any other issues/problems with this program? The program is hard to read and understand. If you show it to a programmer, is it clear what the program is supposed to be doing? The output is just numbers, not very user-friendly. public class hello1 { public static void main(String[] args) { System.out.println(2 * Math.PI * ); System.out.println(Math.PI * Math.pow(20.231, 2)); }
33
Using Variables This code has the same output as the previous version.
public class hello1 { public static void main(String[] args) { double radius = ; double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); } This code has the same output as the previous version. However: The radius is specified only once (better than specifying twice). If you show this program to any programmer, it is fairly obvious what it does (easy to read).
34
Declaring a Variable – One Way
At any point, you can create a variable, by doing a variable declaration. Syntax for variable declaration (one way): type variable_name; For example: String name; int number_of_fingers; double radius; If you declare variables this way, they do not have a value yet. You need to give them a value at some point later in your code.
35
Declaring a Variable – One Way
public class xyz { public static void main(String[] args) { String name; int number_of_fingers; double radius; radius = ; System.out.println(radius); number_of_fingers = 5; name = "Tim"; System.out.println(name); }
36
Declaring a Variable – Second Way
Syntax for variable declaration (second way): type variable_name = initial_value; If you use this way, then when you declare the variable you are also giving it an initial value. For example: String name = "Tim"; int number_of_fingers = 5; double radius = ;
37
Types There are many different types in Java.
However, initially, you just need to know these three: double int String You need to think carefully, and use the correct type for your variable. For integers (positive and negative), use int. For floating point numbers, use double. For text, use String.
38
Variable Names The textbook describes the rules for variable names.
Here is a simplified version: variable names should start with a letter (upper or lower case). variable names should only include letters, numbers, and underscores. variable names are case-sensitive. variable names cannot be equal to reserved words, such as double, class, int, public, …
39
Using Variables After you declare a variable, you can use it in the rest of the code: You can use its value. You can change its value. Setting or changing the value of a variable is called assignment. public class hello1 { public static void main(String[] args) { int candies = 5; System.out.println(candies); candies = 7; candies = candies + 10; } Output:
40
Using Variables After you declare a variable, you can use it in the rest of the code: You can use its value. You can change its value. Setting or changing the value of a variable is called assignment. public class hello1 { public static void main(String[] args) { int candies = 5; System.out.println(candies); candies = 7; candies = candies + 10; } Output: 5 7 17
41
Declarations and Assignments
In this program: Which lines of code are declarations? Which lines of code are assignments? Output: 5 7 17 public class hello1 { public static void main(String[] args) { int candies = 5; System.out.println(candies); candies = 7; candies = candies + 10; }
42
Declarations and Assignments
In this program: Which lines of code are declarations? int candies = 5; Which lines of code are assignments? int candies = 5; candies = 7; candies = candies + 10; Output: 5 7 17 public class hello1 { public static void main(String[] args) { int candies = 5; System.out.println(candies); candies = 7; candies = candies + 10; }
43
Declarations and Assignments
Note that this line is BOTH a declaration AND and an assignment: int candies = 5; Output: 5 7 17 public class hello1 { public static void main(String[] args) { int candies = 5; System.out.println(candies); candies = 7; candies = candies + 10; }
44
Declarations and Assignments
This code will not run. Why? public class hello1 { public static void main(String[] args) { int candies; System.out.println(candies); candies = 7; candies = candies + 10; }
45
Declarations and Assignments
This code will not run. Why? The first time we try to print variable candies, the variable has not been assigned a value yet. You cannot use a variable until it has been assigned a value. Java programs will not run if you have any violation of this principle. public class hello1 { public static void main(String[] args) { int candies; System.out.println(candies); candies = 7; candies = candies + 10; }
46
Fix – One Way One easy way to fix the code is to assign an initial value to candies when we declare it. Output: 5 7 17 public class hello1 { public static void main(String[] args) { int candies = 5; System.out.println(candies); candies = 7; candies = candies + 10; }
47
Fix – Another Way Another way to fix the code is to assign a value to candies after we declare it (but before we use it). public class hello1 { public static void main(String[] args) { int candies; candies = 5; System.out.println(candies); candies = 7; candies = candies + 10; } Output: 5 7 17
48
Returning to the Circles Program
Version with variables: Which lines are declarations? Which lines are assignments? public class hello1 { public static void main(String[] args) { double radius = ; double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); }
49
Returning to the Circles Program
Version with variables: Which lines are declarations? Shown in red. Which lines are assignments? Shown in red. Here each declaration line is also an assignment. public class hello1 { public static void main(String[] args) { double radius = ; double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); }
50
Returning to the Circles Program
Version with variables: Problem: the radius is hardcoded. Why is this a problem? public class hello1 { public static void main(String[] args) { double radius = ; double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); }
51
Problem: Radius is Hardcoded
Why is this a problem? Biggest reason: the user needs to be a programmer. You cannot use this program without changing the program.
52
Solution Allow the user to enter the radius value as input.
53
Revised program with user input. There are several new things here:
import java.util.Scanner; public class hello1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter the radius: "); double radius = in.nextDouble(); double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); } Revised program with user input. There are several new things here: the import line. The Scanner object. The System.out.printf method.
54
The Scanner object allows us to obtain user input.
import java.util.Scanner; public class hello1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter the radius: "); double radius = in.nextDouble(); double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); } The Scanner object allows us to obtain user input. To create a Scanner object, we need to: Put the import statement at the top of the Java file. Create a Scanner object, as shown in the first line of the main method: Scanner in = new Scanner(System.in);
55
import java.util.Scanner;
public class hello1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter the radius: "); double radius = in.nextDouble(); double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); } The System.out.printf method is a more powerful version of the System.out.println method. One difference is that System.out.println always prints a new line at the end, whereas System.out.printf does not.
56
println and printf public class hello1 { public static void main(String[] args) { System.out.println("hello"); System.out.printf("hello\n"); } These two lines do the exact same thing: System.out.println("hello"); System.out.printf("hello\n");
57
Back to the Circles Program
import java.util.Scanner; public class hello1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter the radius: "); double radius = in.nextDouble(); double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); } Please enter the radius: 5.2 Next problem to fix: the output is just numbers.
58
Back to the Circles Program
import java.util.Scanner; public class hello1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter the radius: "); double radius = in.nextDouble(); double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); } Please enter the radius: 5.2 The circumference is The area is It would be nicer to have output like this:
59
Circles Program with (Somewhat) Nicer Output
import java.util.Scanner; public class hello1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter the radius: "); double radius = in.nextDouble(); double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.printf("The circumference is %f\n", circumference); System.out.printf("The area is %f\n", area); }} Please enter the radius: 5.2 The circumference is The area is
60
System.out.printf public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; double temperature = 85.3; System.out.printf("There are %d days in %s\n", days, month); System.out.printf("Average temperature in %s: %f degrees\n", month, temperature); } There are 31 days in July Average temperature in July: degrees Output: System.out.printf gives you an easy way to print nicer output, by combining text, variables, and other values.
61
System.out.printf printf works as follows:
public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; double temperature = 85.3; System.out.printf("There are %d days in %s\n", days, month); System.out.printf("Average temperature in %s: %f degrees\n", month, temperature); } printf works as follows: It starts printing the text in the first argument. There are
62
System.out.printf printf works as follows:
public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; double temperature = 85.3; System.out.printf("There are %d days in %s\n", days, month); System.out.printf("Average temperature in %s: %f degrees\n", month, temperature); } printf works as follows: It starts printing the text in the first argument. When it finds the first % sign, it prints the second argument. There are 31
63
System.out.printf printf works as follows:
public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; double temperature = 85.3; System.out.printf("There are %d days in %s\n", days, month); System.out.printf("Average temperature in %s: %f degrees\n", month, temperature); } printf works as follows: It starts printing the text in the first argument. When it finds the first % sign, it prints the second argument. It continues printing text. There are 31 days in
64
System.out.printf printf works as follows:
public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; double temperature = 85.3; System.out.printf("There are %d days in %s\n", days, month); System.out.printf("Average temperature in %s: %f degrees\n", month, temperature); } printf works as follows: It starts printing the text in the first argument. When it finds the first % sign, it prints the second argument. It continues printing text. When it finds the second % sign, it prints the third argument. There are 31 days in July
65
System.out.printf printf works as follows:
public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; double temperature = 85.3; System.out.printf("There are %d days in %s\n", days, month); System.out.printf("Average temperature in %s: %f degrees\n", month, temperature); } printf works as follows: It starts printing the text in the first argument. When it finds the first % sign, it prints the second argument. It continues printing text. When it finds the second % sign, it prints the third argument. And so on, until the entire text is processed.
66
System.out.printf public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; System.out.printf("There are %d days in %s\n", days, "July"); System.out.printf("Average temperature in %s: %f degrees\n", month, ( ) / 2.0); } The values that you provide in the second argument, third argument, and so on, can be: variables, like days in the example above. constants, like "July" in the example above. expressions, like ( ) / 2.0 in the example above.
67
Format Specifiers %d, %f, %s are called format specifiers.
public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; System.out.printf("There are %d days in %s\n", days, "July"); System.out.printf("Average temperature in %s: %f degrees\n", month, ( ) / 2.0); } %d, %f, %s are called format specifiers. A format specifier must match the value that will be printed. %d is for values of type int %f is for values of type double %s is for values of type String We will see a few more format specifiers soon.
68
Another Example: Converting Weeks to Days
Let’s write a program that: Asks the user to enter a number of weeks. Converts that number of weeks to a number of days. double G = 6.694E-11
69
Another Example: Converting Weeks to Days
import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter number of weeks: "); int weeks = in.nextInt(); int days = weeks * 7; System.out.printf("There are %d days in %d weeks\n", days, weeks); } double G = 6.694E-11
70
Another Example: Computing the Average of Three Numbers
Let’s write a program that: Asks the user to enter three numbers. Computes and prints out the average of those numbers. double G = 6.694E-11
71
Another Example: Computing the Average of Three Numbers
import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter the first number: "); double n1 = in.nextDouble(); System.out.printf("Please enter the second number: "); double n2 = in.nextDouble(); System.out.printf("Please enter the third number: "); double n3 = in.nextDouble(); double average = (n1 + n2 + n3) / 3.0; System.out.printf("The average is %f\n", average); } double G = 6.694E-11
72
Another Example: Computing the Gravity Force
Law of gravity: 𝐹=𝐺 𝑚 1 𝑚 2 𝑟 2 F: force of gravity between two objects. m1: mass of the first object. m2: mass of the second object. r: distance between the two objects. Let’s write a program that applies the law of gravity to compute the gravity force between two objects. Asks the user to enter the masses of the two objects. Asks the user to enter the distance between the two objects. Computes and prints out the gravity force. double G = 6.694E-11
73
Another Example: Computing the Gravity Force
import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter the first mass: "); double m1 = in.nextDouble(); System.out.printf("Please enter the second mass: "); double m2 = in.nextDouble(); System.out.printf("Please enter the radius: "); double r = in.nextDouble(); double G = 6.694E-11; double gravity = G * m1 * m2 / (r * r); System.out.printf("The gravity force is %f\n", gravity); } double G = 6.694E-11
74
Comments /* A program that converts weeks into days. Written on 7/15/2015. */ import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter number of weeks: "); int weeks = in.nextInt(); // Here is where we convert weeks into days. int days = weeks * 7; System.out.printf("Result: %d days\n", days); } Comments allow you to make notes on the program for yourself, and for other people reading your code. Comments are ignored by Java. Single line comments: they start with // (see line in green above) Multiple-line comments: they start with /*, end with */ (see lines in red)
75
Comments import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); // Create scanner object. System.out.printf("Enter number of weeks: "); int weeks = in.nextInt(); // Get user input int days = weeks * 7; // Converting weeks into days. System.out.printf("Result: %d days\n", days); } Comments starting with // can be placed at the end of a line (see code marked in red)
76
Some Guidelines To learn how to code, you need PRACTICE.
What will usually not work: Listen to the lectures. Go and try to do the assignments. What will usually work: Listen to the lectures and KEEP NOTES. Actually run every piece of code that we do in class. Understand every line of every piece of code we do in class. Think of variations of what we do in class, and try them. Predict what the variation will do, and verify by running it. Then try the assignments.
77
Some Guidelines You need to understand the terminology:
method, string, double, int, main class name, numerical expression, variable, declaration, assignment, newline character You will encounter many terms in this course. YOU NEED TO LEARN EXACTLY WHAT THEY MEAN. DO NOT RELY ON ENGLISH. These terms have meanings in conversational English that are only vaguely related with their meaning in programming.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.