You are on page 1of 26

CS 1301 – COMPUTER SCIENCE I

Data types; variables; assignments


Schedule
◦ SI this week
◦ Lab 4 due tonight
◦ Project 1 assigned today, due Wednesday
September 11
◦ Exam 1 on Monday September 9. Bring a pencil
◦ No SI next week
Last Class
◦ Methods
◦ Purpose of methods
◦ Specifications, header, body
◦ Order of execution
◦ Methods calling other methods
◦ Principles in writing methods
◦ DRY
◦ SRP
◦ Helper methods
Today’s class
◦ Method input and output
◦ Data types
◦ Variables
◦ Print statements
◦ Project 1
Methods
◦ Used to have an object do something.
◦ Implements its behavior or provide a service
◦ Compute a value.
◦ May need to pass data to it so it can do its job.
◦ Parameters (input)
◦ May return a value
◦ A return statement is used to return data from a
method.
◦ Example
return cost;
Method inputs and outputs
◦ A method is a behavior/action/operation an
object can perform.

• Input – Parameters
• Example:
void setDirection(double direction)
• Output – Return type
• Example
void setDirection(double direction)
boolean isTailDown()
Variables
◦ Data is stored in variables
◦ A variable is a memory location with a name
attached to it
◦ Can think of a ‘box’ inside the computer with the
name written on the outside of the box
◦ Variables must be declared before they can be
used
◦ What’s ‘in the box’ is the value of the variable
◦ Java needs to know what type of data you want
to store
Data types
◦ Java must know the type of data with which it is
working.
◦ All data can be categorized by its type.
◦ Types used this semester:
◦ int used for integers
◦ double used for decimal numbers
◦ String used for words or sentences
◦ boolean used for true or false
Primitive vs Object
◦ int, double, and boolean are known as primitive
data types
◦ No methods
◦ Don’t use new operator to create a new
◦ Strings are objects
◦ Lots of methods to manipulated Strings
◦ Don’t need to used the new operator (The only
exception to this requirement)
Declaration Statements
◦ Before variables can be used they must be
declared
type name;
Example declarations
◦ int x;
◦ int y;
◦ int count;
◦ double temperature;
◦ double cost;
◦ boolean headlightsOn;
◦ String message;
Fields
◦ When the variables are attributes in of a class
they are fields
◦ AKA
◦ Instance variables
◦ Data members

private String name;


private int idNumber;
Assignments Statements
◦ An assignment statement changes the value of a
variable
name = expression;

◦ When the assignment statement is executed the


value of expression is determined and stored in
name
Expressions
◦ There are lots of different kinds of expressions:
◦ Literals:
◦5
◦ 123.456
◦ “Hello World”
◦ true, false
◦ Math expressions: used to perform a calculation
◦ Method calls
◦ String expressions: create new Strings out of old
Strings
Examples of assignments
◦ x = 5;
◦ y = x + 4;
◦ x = x + 1;
◦ count = stadium.getCount();
◦ temperature = 98.6;
◦ headLightsOn = false;
◦ message = “Attendence: “ + count;
◦ this.name = “Suzy Smith”;
Examples
int x;
int y;
double z;
x = 3 + 5;
y = x*2;
z = (3.14 * 2) / 1.5; //floating point division
String message = “Hello” + “ Suzy”;
x = y;
x = x/3; // integer division
X = y%3; // remainder when y is divided by 3
A method with input and output

private double getTotalTax(double costOfPurchase){


double taxRate = 0.07;
double total = costOfPurchase * taxRate;
return total;
}
Compute the average of two ints

/**
* Computes the average of two ints as a double
* @precondition none
* @postcondition none
* @param first the first int
* @param second the second int
* @return average the average
*/
public double average(int first, int second) {
// Code codes here
}
Print statements
◦ Prints a String to the output console
◦ System.out.println(string);
◦ Prints string to the screen followed by a carriage
return

◦ System.out.print(string);
◦ Prints string to the screen and cursor remains at
end position
Shorthand operators
◦ Incrementing a variable
◦ Common to increment by one
x = x + 1; // Increments by one
◦ Can also be written as
x++;
◦ Assignment shortcuts
◦ Common to update a variable
x = x + 5; // Adds 5 to x
◦ Can also be written as
x+=5;
x += 5; // White space doesn’t matter
Converting between types
◦ Need to assign like types to like types
int x, y;
double z;
boolean valid;
String result;

x = 3.4; // Produces a compilation error


z = 3; // Legal
y = 5/2.0; // Produces a compilation error
x = 1.0; // Produces a compilation error
valid = true; // Legal
valid = “false”; // Produces a compilation error

result = “Good job”; // Legal


result = 2; // Produces a compilation error
result = “2”; // Legal
Assignment conversion
◦ Java will do some data type conversions
automatically for you
◦ Will convert an int to a double for you, but
not vice versa.

int x, y;
double z;
x = 3;
y = 5;
z = x; // Legal
z = y + 3.2; // Legal
x = 3.2; // Illegal
String operators
◦ Java will do some conversions to a String for you
int x = 3;
double z = 5.2;
String result;
result = x; // Illegal
result = “Result: ” + x; // Legal as one of
operands is a string so will automatically
convert the x to a string for you, result
equals “Result: 3” after assignment
Casting
◦ Forcing one type to another
◦ Typically used to convert a double to an int
◦ Beware of doing this as will be losing precision.

int x = 3;
double z = 5.2; Type cast
x = z; // Illegal
x = (int)z; // Forces the double to be an int, but
will lose the fractional portion, x equals 5 after
the assignment
Next class
◦ Exam 1

You might also like