Você está na página 1de 15

Java only one time compilation, can run anywhere across many platforms.

Eclipse is an IDE, allows smart editing of java code. Simple environment, and allows
SVN to store code in servers. Works across windows, linux, mac..
Src source code
Variables: Integer, double, string, Boolean
Order of execution is important
public class class2 {
public static void main(String[] args){
int a=2;
int b=3;
a=a+b;
b=a+b;
System.out.println(" a= " +a+" , b=" +b);
}
}
a= 5 , b=8
public class class2 {
public static void main(String[] args){
int a=2;
int b=3;
b=a+b;
a=a+b;
System.out.println(" a= " +a+" , b=" +b);
}
}
a= 7 , b=5
Double variable:
Memory is created in bigger size to store floating point numbers.
public class class2 {
public static void main(String[] args){
//to calculate a circle
double radius=10.5;
double area=Math.PI *radius*radius;
System.out.println("Area is " +area);
}
}
Area is 346.36059005827474

public class class2 {


public static void main(String[] args){
//to calculate interest

double deposit=10000.0;
double APR=0.025;
deposit=deposit*(1+APR)*(1+APR)*(1+APR);
}
}

System.out.println("Value is " +deposit);

Value is 10768.906249999996

String variable
public class class2 {
public static void main(String[] args){
/*string variables
creates space to store characters
a is a string type variable in this program
*/
String a="Hello World";
System.out.println(a);
}
}

Concatenation
public class class2 {
public static void main(String[] args){
//concatenation example (there is a mistake in the output)
String x="I have "+ 5+5+ "toes";
System.out.println(x);
}
}
I have 55toes
This is because execution happens from left to right. We must use the parenthesis to change
the order of execution.
public class class2 {
public static void main(String[] args){
//concatenation example has an
String x="I have "+ (5+5)+ "toes";
System.out.println(x);
}
}
I have 10toes

Boolean example
public class class2 {
public static void main(String[] args){
// boolean example

int age=22;
//check if age is divisible by 5
boolean medexam=((age%5)==0); // should take exam if age is multiple of 5
}
}

System.out.println(medexam);

Operators AND OR NOT


public class class2 {
public static void main(String[] args){
// boolean example
int age=22;
//check alcohol eligibility age
boolean check=((age>=21)&&(age<=60));
System.out.println(check);
}
}

public class class2 {


public static void main(String[] args){
// boolean example, alcohol is allowed either if age>21 or person is a student
int age=15;
boolean isStudent = true;
//check alcohol eligibility age
boolean check=((age>=21)||(isStudent));
System.out.println(check);
}
}
public class class2 {
public static void main(String[] args){
// to check if a given year is a leap year.
// year should be divisible by 4 and not by 100... but 100 and 400 is a yeap year
int year = 2016;
boolean check=false;
if((year%4)==0)
if((year%100)==0)
if((year%400)==0)
check=true;
else
check=false;
else
check=true;
else
check=false;
System.out.println(check);
}
}

Data type conversions, double to int (not recommended)


public class class2 {
public static void main(String[] args){
// data type conversions, this is not recommended
int a=5;
double b=4.8;
a=(int)b/2;
System.out.println(a +" "+ b);
}
}

Method is a way to encapsulate a computing procedure into an interface. It allows


easy reusability.
For example, we can have a method called area and use that to calculate the area
of several circles instead of just one.
Method has return type
Method has input values
Method has a name
public class class2 {
public static void main(String[] args){
double r=1;
double area=circleArea(r);
System.out.println(area);
double x=3;
double y=4;
double h=hypotenuse(x,y);
System.out.println(h);
}
// Method
public static double circleArea(double radius){
double area;
area=Math.PI*radius*radius;
return area;
}
public static double hypotenuse(double a,double b)
{
double h=Math.sqrt((a*a)+(b*b));
return h;
}
}
02/04/2015

MID TERM EXAM Module 1-3 on 23rd Feb 2015, 6:30-8:30pm, Closed Book
If statements
Concept of sequential execution
public class class2 {
public static double absolute(double x)
{// output the absolute value of x
if(x<0)
x=-x;
return x;
}
public static void main(String[] args)
{
double x=-4.5;
System.out.println(absolute(x));
}
}
public class class2 {
public static double max(double x,double y)
{// computes the maximum of two doubles
if(x>y)
return x;
else
return y;
}
public static void main(String[] args)
{
double x=11.5;
double y=8.7;
System.out.println(max(x,y));
}
}
Swap two numbers
public class class2 {
public static void main(String[] args)
{
double x=2.9;
double y=3.5;
x=x+y;
y=x-y;
x=x-y;
System.out.println(x + " " + y);
Math.random
public class class2 {

public static void main(String[] args)


{
double x;
for(int i=1;i<20;++i)
{
x=Math.random();
if(x>0.5)
System.out.println("Heads");
else
System.out.println("Tails");
}
}
}
While loop
public class class2 {
// Time taken to make the deposit become $2000
public static void main(String[] args)
{
double deposit=1000;
int year=0;
while(deposit<2000)
{
++year;
deposit=deposit*1.03; // Deposit increase by 5%
}
System.out.println(Math.round(deposit)+ " " + year);
}
}

While(Boolean expression)
{
Statements
}

It is an entry controlled loop


public class class2 {
public static void main(String[] args)
{
int i=1,j=2;

while(i<10)
{
System.out.println(i+ " "+ j);
j=j*2;
++i;
}

}
public class class2 {
public static void main(String[] args)
{
for(int i=1,j=2;i<10;++i,j=j*2)
{
System.out.println(i+ " "+ j);

}
public class class2 {
// Use FOR loop for calculating the sum of first 10 integers
public static void main(String[] args)
{
int sum=0;
for(int i=1;i<=10;++i)
{
sum=sum+i;

}
System.out.println(sum);
}
}

Print out 5 * in a line


import java.util.Scanner;
public class Class {
public static void main(String[] args)
{
for(int i=0;i<5;++i)
{
System.out.print("* ");

}
}
}

02/11/2015
Module 3 ARRAYS
Printing * as a square of size n
import java.util.Scanner;
public class Class {
public static void main(String[] args)
{
System.out.println("Enter the number: ");
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
for(int i=0;i<n;++i)
{
for(int j=0;j<n;++j)
{
System.out.print("* ");
}
System.out.print("\n");
}
}
}
TRIANGLE
import java.util.Scanner;
public class Class {
public static void main(String[] args)
{
System.out.println("Enter the number: ");
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
for(int i=0;i<n;++i)
{
for(int j=0;j<i;++j)
{
System.out.print("* ");
}
System.out.print("\n");
}
}
}

INPUT AND OUTPUT

02/18/2015
Java has libraries to communicate with the OS.

METHODS
03/05/2015

Methods avoid the need to write the same program repeatedly.


Method to calculate the square root of a double

import java.util.Scanner;
import java.text.*;
public class Class {

public static double sqrt(double c)


{
double x;
if(c<0)
return Double.NaN;
else{
return Math.sqrt(c);
}
}
public static void main(String[] args) {

}
}

Methods provide a new way to control the flow of execution.


Methods pass by value, so the original variable remains unaffected.

Method with void return type doesnt return any value. Void methods have a side
effect.

Returning multiple values, return an array.


Scope of variables
For(int i=0
{
}
This variable I has local scope, only exists within the loop.
PASS BY VALUE
import java.util.Scanner;
import java.text.*;
public class Class {

public static int triple(int i)


{
return i+3;
}
public static void main(String[] args) {
int i=5;
triple(i);
System.out.println(i);

}
}
Output will be 5 only since it is passed by value. (no side effect)

import java.util.Scanner;
import java.text.*;

public class Class {

public static void triple(int[] a)


{
for(int i=0;i<a.length;++i)
{
a[i]=a[i]*3;
}
}
public static void main(String[] args) {
int[] a={1,2,3};
triple(a);
System.out.println(a[0]);

}
}

OUTPUT
3,6,9

Arrays are always passed by reference.


Strings are passed by reference.
03/25/2015

OBJECTS

Object oriented programming


API provides inner details, when we buy a TV, we dont have to know how the
remote actually works, we just use it.
In java programming, our task is to define our own object. We have seen simple
data types like int float, string.etc and we know their operations..
OOP allows us to define our own data types. Clients only see them as API.

Constructors are used to initialize.


Object has --- Variables, methods and constructors.
Color is a data type, there are several methods within this object.

import java.awt.Color;
public class AlbersSquares {
public static void main(String[] args) {
int r1 = Integer.parseInt(args[0]);
int g1 = Integer.parseInt(args[1]);
int b1 = Integer.parseInt(args[2]);
Color c1 = new Color(r1, g1, b1);
int r2 = Integer.parseInt(args[3]);
int g2 = Integer.parseInt(args[4]);
int b2 = Integer.parseInt(args[5]);
Color c2 = new Color(r2, g2, b2);
StdDraw.setPenColor(c1);
StdDraw.filledSquare(.25, .5, .2);
StdDraw.setPenColor(c2);
StdDraw.filledSquare(.25, .5, .1);

}
}

StdDraw.setPenColor(c2);
StdDraw.filledSquare(.75, .5, .2);
StdDraw.setPenColor(c1);
StdDraw.filledSquare(.75, .5, .1);

Difference in luminance between 2 colors should be > 128 for one to be seen
in the background of another

public static Color toGray(Color c) {


int y = (int) Math.round(lum(c));
Color gray = new Color(y, y, y);
return gray;
}
04/08/2015
ABSTRACT DATA TYPES
Interface tells us what the class can do.
Implements keyword in java.
Class circle implements shape . This means that circle has all the methods of
shape. (implements all the methods)
Class implements an interface. One interface can be implemented by many
classes.
List is an ordered sequence of objects.
List should support add(x), get(i), remove(x) and set(I,x)
Set contains(x) is a Boolean function
Add(x) to add an element in a set if it isnt present. Set doesnt allow duplicate
values.
List<T> is an interface
ArrayList<T> us a class Linkedlist<T> are two ways to implement the list
interface.
T is a special feature type of the object.
List is a sequence of items of the same type T.
Define a list
List<Circle>

// list of circles

List<String> // list of strings


Parameterized interface --- T can be replaced by any datatype/class name
List<Integer>

-- int isnt a class name

List<Double>

Example
List<Circle> list;
List = new ArrayList>Circle>();
For(int i=0;i<10;++i)
{
Circle c= new Circle(i*1.0);
List.add(c);
}
Circle c= new Circle(100.0);
List.set(1,c) // will set the second circle as 100 radius.
For(int i=0;i<List.size();++i)
{
Circle c= list.get(i)
c.draw(0,0);

ListIterator . Vs List with for loops


Iterator can be convenient in removing stuff

Set
Set<Integer> A = new HashSet>Integer>(); //TreeSet etc
A.add(0);
A.add(1);
A.add(2);

For(int i:A)
{

System.out.println(i); // prints all contents of A


}

CODE TO COMPUTE THE UNION OF 2 SETS

MAP is a pair of <key,value>


SSN and Score mapped together
KEY must be unique.
Get(mySSN) ---- returns the credit score
We can remove a pair with a key

Last Lecture
04/15/2015
Computer program = Data Structure + Algorithms
References in Java

Você também pode gostar