SD1x-1 FinalDeck Revised 6.8.17
SD1x-1 FinalDeck Revised 6.8.17
SD1x-1 FinalDeck Revised 6.8.17
1.1
Arvind Bhusnurmath
Property of Penn Engineering, Arvind Bhusnurmath SD1x-1 1
Java
• Launch Eclipse
Pick
the
default
worksp
Arvind Bhusnurmath
Property of Penn Engineering, Arvind Bhusnurmath SD1x-1 8
Hello World!
System.out.println(“Hello World!”);
}
}
Run the program by clicking ”Run” in the menu.
Declaring
a
variable
double distance;
String firstName;
• char ch = ‘a’;
• String name = “Superman”;
• Difference between single characters and a string
of characters.
if (x < 5) {
System.out.println(“less than
5”);
}
else {
System.out.println(“more than
5”);
}
If the condition is satisfied do everything within the first set of braces,
otherwise execute statements within the second set of braces.
if (condition1) {
//code block 1
if (condition2) {
//code block 2
}
}
Arvind Bhusnurmath
Property of Penn Engineering, Arvind Bhusnurmath SD1x-1 21
Loops
int sum = 0;
int number = 1;
while (number <= 100) {
sum = sum + number;
number = number + 1;
}
int sum = 0;
for (int number = 1; number <= 100;
number = number + 1) {
sum = sum + number;
}
System.out.println(sum);
int sum = 0;
for (int number = 1; number <= 100;
number = number + 1) {
sum = sum + number;
}
System.out.println(sum);
int sum = 0;
for (int number = 1; number <= 100;
number = number + 1) {
sum = sum + number;
}
System.out.println(sum);
int sum = 0;
for (int number = 1; number <= 100;
number = number + 1) {
sum = sum + number;
}
System.out.println(sum);
Arvind Bhusnurmath
Property of Penn Engineering, Arvind Bhusnurmath SD1x-1 32
Sample Problem
Goal : Write a program that gets a single positive
integer from the user and checks whether it is prime
or not.
import java.util.Scanner;
public class Prime {
public static void main(String
args[]){
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter a number:
int myNumber = scanner.nextInt();
}
}
Arvind Bhusnurmath
Property of Penn Engineering, Arvind Bhusnurmath SD1x-1 36
Syntactic Style
Arvind Bhusnurmath
Property of Penn Engineering, Arvind Bhusnurmath SD1x-1 50
Topics
• What is a class?
• Instance variables
• Methods
• Using a class
myCar.make = “Audi” ;
myCar.model = “A4”;
myCar.year = 2014;
myCar.miles = 0;
myCar.isNew = true;
myCar.owner = “Arvind”;
Arvind Bhusnurmath
Property of Penn Engineering, Arvind Bhusnurmath SD1x-1 63
Topics
• What is a constructor?
• “this” keyword
• Using inbuilt classes
this.name = name ;
this.male = male ;
}
} Property of Penn Engineering, Arvind Bhusnurmath SD1x-1 68
The this keyword
SD1x-1 70
StringBuilder
Arvind Bhusnurmath
Property of Penn Engineering, Arvind Bhusnurmath SD1x-1 72
Topics
Example:
boolean isAdult(int age) {
int magicAge = 21;
return age >= magicAge;
}
Example:
double average(int a, int b) {
return (a + b) / 2.0;
}
if (x > y) {
int larger = x;
}
else {
int larger = y;
}
return larger;
Property of Penn Engineering, Arvind Bhusnurmath SD1x-1 77
Returning a result from a method
System.out.print (john.getName());
System.out.println(" is having a birthday!");
john.birthday();
// methods
Arvind Bhusnurmath
Property of Penn Engineering, Arvind Bhusnurmath SD1x-1 85
Topics
double jan1 = …;
double jan2 = …;
…
This is tedious!
Solution : Array!
0 0 0 0 0 0 0 0 0 0
rainfall
rainfall[0]
0 0 0 0 0 0 0 0 0 0
rainfall
rainfall[1]
0 0 0 0 0 0 0 0 0 0
rainfall
rainfall[rainfall.length - 1]
double sum = 0;
for (int i = 0; i < rainfall.length; i++)
{
sum += rainfall[i];
}
avgRainfall = sum / rainfall.length