Algorithms and Java Basics: Pseudocode, Variables, Assignment, and Interactive Programs
Algorithms and Java Basics: Pseudocode, Variables, Assignment, and Interactive Programs
Course website:
www.csc.villanova.edu/~map/1051/
Some slides in this presentation are adapted from the slides accompanying:
• Java Software Solutions by Lewis & Loftus
• Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne
Source: http://xkcd.com/627/
Algorithm:
Java Program è
******************************************************
import java.util.Scanner;
Algorithm //-------------------------------------------------
// Inputs quality points and credits and calculate
//-------------------------------------------------
{
è
// input credits
1. qp = input from user System.out.print ("Enter Credits > ");
2. credits = input from user credits = scan.nextInt();
// print GPA
System.out.println ("\n\tGPA: " + gpa);
}
}
CSC 1051 M.A. Papalaskari, Villanova University
Next: A closer look at variables & input in Java
Interactive Programs – Input/Output
• Programs can use data obtained during runtime, eg:
int age;
String name;
String name;
Scanner object
import java.util.Scanner;
2. In your main method, before doing any input, declare and
initialize the Scanner object
Scanner scan = new Scanner(System.in);
3. Input away!
System.out.print(“Enter your name”);
name = scan.nextLine();
type variable
int age; declaration statement
literal
age = 18; assignment statement
double x = 3.2, y = -0.80;
combined declaration and assignment statement
final int INCHES_PER_FOOT = 12;
constant declaration (always initializes value)
String name = scan.nextLine();
input from user
CSC 1051 M.A. Papalaskari, Villanova University
Variable Declaration
int age;
double x, y;
String name;
17 add, subtract,
int integers 12345 multiply, divide
floating-point 3.1415 add, subtract,
double numbers 6.022e23 multiply, divide
true
boolean truth values false
and, or, not
total = 55 - discount;
int totalSeconds =
(hours * 3600) + (minutes * 60) + seconds;
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
14 / 3 14 % 3
143 / 60 143 % 60
20 / 16 20 % 16
8 / 12 8 % 12
more examples:
a + b + c + d + e a / (b + c) - d % e
a – b / c + d * e a / (b * (c + (d - e)))
CSC 1051 M.A. Papalaskari, Villanova University
Tracing the values of variables after each statement.
age
int age = 18;
18
x
double x;
?
x = 0.5;
0.5
x = x + 0.2; 0.7
age x name
________________________________________
int age = 18;
18
double x; 18 undefined
Final values:
CSC 1051 M.A. Papalaskari, Villanova University
Trace: TRY THIS:
int a, b;
a = 3;
b = 4;
a = b;
double pi = 3.14;
Final values:
a b pi
CSC 1051 M.A. Papalaskari, Villanova University
Trace: TRY THIS:
int a, b;
a = 3;
b = 4;
int c = a;
a = b;
b = 5;
b = c;
Final values:
a b c
CSC 1051 M.A. Papalaskari, Villanova University
Assignment operator
davesAge = 22;
type variable
int age; declaration statement
literal
age = 18; assignment statement
double x = 3.2, y = -0.80;
combined declaration and assignment statement
final int INCHES_PER_FOOT = 12;
constant declaration (always initializes value)
String name = scan.nextLine();
input from user
CSC 1051 M.A. Papalaskari, Villanova University