JAVA
import [Link];
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner([Link]);
[Link]("what's your name:");
String name = [Link]();
[Link]("what is your age:");
int age= [Link]();
[Link]();
[Link]("what's your fvt food:");
String food = [Link]();
[Link]("Hello "+name);
[Link]("you are "+age);
[Link]("you like "+food);
[Link]();
}
}
[Link]() to clear the scanner so we can entrer a string
after the int
public class Main{
public static void main(String[] args) {
double f =10;
f=(int)f/3; //casting
[Link](f);
Casting
GUI
import [Link];
public class Main{
public static void main(String[] args) {
String name = [Link]("Enter your name");
int age =[Link]([Link]("Enter your
age ")); //parseDouble
[Link](null,"Hello "+name+"you are "+age);
Math Class
public class Main{
public static void main(String[] args) {
double x=58.32;
double y=159.25;
double z = [Link](x, y); //min(x,y) abs(x) sqrt(y)
//round(x) //ceil(x) //floor(x)
[Link](z);
Random
import [Link];
public class Main{
public static void main(String[] args) {
Random r = new Random();
int x = [Link](6)+1;
[Link](x);
If statements
if(){}
else if(){}
else{}
Switch(day){
case “monday” : syso(“it is Monday”);
break;
…..
default : syso(“that’s not a day);
}
Logical Operators
&& and
|| or
! not
While loop
import [Link];
public class Main{
public static void main(String[] args) {
Scanner s = new Scanner([Link]);
[Link]("Enter your name:");
String name = [Link]();
while([Link]()) {
[Link]("Enter your name:");
name = [Link]();
}
[Link](name);
[Link]();
Do Loop
import [Link];
public class Main{
public static void main(String[] args) {
Scanner s = new Scanner([Link]);
String name;
do {
[Link]("Enter your name:");
name = [Link]();
}while([Link]());
[Link](name);
[Link]();
}
}
for loop
public class Main{
public static void main(String[] args) {
for(int i=10; i>=0; i--) {
[Link](i);
}
[Link]("Hello");
}
}
public class Main{
public static void main(String[] args) {
for(int i=0; i<=10; i++) {
[Link](i);
}
[Link]("Hello");
}
}
Arrays
public class Main{
public static void main(String[] args) {
String[] cars = {"Camaro","Tesla","Corvette"};
[Link](cars[0]);
}
}
or
public class Main{
public static void main(String[] args) {
String[] cars = new String[3];
cars[0]="Camaro";
cars[1]="Corvette";
cars[2]="Tesla";
for(int i=0; i<[Link]; i++) {
[Link](cars[i]);
}
}
2D Arrays
import [Link];
public class Main{
public static void main(String[] args) {
Integer[][] cars = new Integer[3][3];
Scanner s = new Scanner([Link]);
for(int i=0; i<3; i++) {
for(int j =0; j<3; j++) {
[Link]("Cars["+(i+1)+"]["+(j+1)+"]:");
cars[i][j]=[Link]();
}
}
for(int i=0; i<3; i++) {
for(int j =0; j<3; j++) {
[Link]("Cars["+(i+1)+"]["+(j+1)+"]="+cars[i][j]);
}
}
[Link]();
}
}
Strings
public class Main{
public static void main(String[] args) {
String name = "AYA";
boolean res = [Link]("AyA");//or just equals
//int res = [Link]();
//char res = [Link](0);
//int res = [Link]("a");
//boolean res = [Link]();
//String res = [Link]();
//String res = [Link]();
//String res = [Link](); To remove Spaces
//String res= [Link](oldchar,newChar); [Link]('o','a');
[Link](res);
}
}
}
ArrayList
a resizable array
import [Link];
public class Main{
public static void main(String[] args) {
ArrayList<String> food = new ArrayList<String>();
[Link]("pizza");
[Link]("Hamburger");
[Link]("hotdog");
for(int i=0; i<[Link](); i++) {
[Link]([Link](i));
}
}
}
//[Link](0,"suchi");
//[Link](2);
//[Link]();
for -each loop
less steps, more readable
import [Link];
public class Main{
public static void main(String[] args) {
ArrayList<String> food = new ArrayList<String>();
[Link]("pizza");
[Link]("Hamburger");
[Link]("hotdog");
for(String i : food) {
[Link](i);
}
}
}
Methods
public class Main{
public static void main(String[] args) {
String name="AYA";
hello(name);
static void hello(String name) {
[Link]("Hello "+name);
}
}
Overloaded Methods
methods that share the same name but have different
parameters
method name+ parameters= method signnature
printf
%b boolean
%c char
%s String
%d int
%f double
Final Keyword
final double pi = 3.14; //Unchangeable
OOP : Object Oriented Programming
Object : an instance of a class that my contain attributes and
methods
Constructors
special method that’s called when an object is
instantiated(created)
file 1
public class Human {
String name;
int age ;
double weight;
Human(String name, int age , double weight){
[Link]=name;
[Link]=age;
[Link]=weight;
void eat() {
[Link]([Link]+" is eating");
}
file 2
public class Main{
public static void main(String[] args) {
Human h = new Human("aya",21,59.0);
[Link]();
}
}
Overloaded Constructors
the same name but have different parameters
they have there own unique signature
toString method
file1
public class Main{
public static void main(String[] args) {
Human h = new Human();
[Link]([Link]());// or just h
}
}
file2
public class Human {
String name="aya";
int age=21;
double weight=59.00;
@Override
public String toString() {
return name + "\n" +age+"\n"+weight;
}
Inheritance
extends
Method Overriding
Declaring a methid in sub class which is already present in
parent class so that the child class can give it’s own
implementation
super Keyword
Abstraction
abstract classes cannot be instantiated, but they can have a
subclass abstract methods are declared without
implementation
public abstract class Vehicle {
abstract void go();
public class Car extends Vehicle{
@Override
void go() {
// TODO Auto-generated method stub
[Link]("Car is going");
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car c= new Car();
[Link]();
}
}
Access Modifiers class subclass package world
public y y y y
protected y y y
no modifiers y y
Private y
Encapsulation
import [Link];
public class Car{
private String name;
private double salary;
public Car() {
Scanner s= new Scanner([Link]);
[Link]("Enter your name:");
[Link]([Link]());
[Link]("Enter your salary");
[Link]([Link]());
[Link]();
}
public String getName() {
return [Link];
}
public Double getSalary() {
return [Link];
}
public void setName(String name) {
[Link]=name;
}
public void setSalary(double salary) {
[Link]=salary;
}
}
Copy Objects
public class Car{
private String name;
private double salary;
public Car(String name,double Salary) {
[Link](name);
[Link](Salary);
public String getName() {
return [Link];
}
public Double getSalary() {
return [Link];
}
public void setName(String name) {
[Link]=name;
}
public void setSalary(double salary) {
[Link]=salary;
}
public void copy(Car x) {
[Link]([Link]());
[Link]([Link]());
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car c1= new Car("aya",2000.0);
Car c2 = new Car("hamza",2001.0);
[Link](c1+"\n"+c2+"\n");
[Link]([Link]()+"\
n"+[Link]());
[Link]([Link]()+"\
n"+[Link]());
[Link](c1); //c2=c1
[Link](c1+"\n"+c2+"\n");
[Link]([Link]()+"\
n"+[Link]());
[Link]([Link]()+"\
n"+[Link]());
}
IN
Interface
public interface Flee {
public void flee();
}public class Car implements Flee,...{
public Car() {
}
@Override
public void flee() {
[Link]("The car is fleeing");
}
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
[Link]();
Polymorphism
the ability of an object to identify as more than one type
Vehicle[] racers= {cars,bicycle}
cars and bicycle are subclasses of Vehicle
Or
Object[] racers= {cars,bicycle}
Exception handling
an event that accurs during the execution of a program
that,disrupts the normal flow of instructions
import [Link];
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner([Link]);
int a,b;
try { [Link]("Enter a number:");
a= [Link]();
[Link]("Enter a number:");
b=[Link]();
[Link](a/b);}
catch(ArithmeticException e) {
[Link](e);
}
finally {
[Link]("always printed");
[Link]();
}
FILE
import [Link];
public class Main {
public static void main(String[] args) {
File f = new File("/home/aya/Desktop/notes/[Link]");
if([Link]()) {
[Link]("That file exists");
[Link]([Link]());
[Link]([Link]());
[Link]([Link]()
[Link]();
//[Link]();
}else {
[Link]("that file doesn't exist");
}
File Writer
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("[Link]");
[Link]("AYA EL JAHIDI");
[Link]("\n 21 years old");
[Link]();
} catch (IOException e) {
// TODO Auto-generated catch block
[Link]();
}
}
}
FileReader
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("[Link]");
int data = [Link](); //when read() return -1 there is no
more data to read
while(data != -1) {
[Link]((char)data);
data = [Link]();
}
[Link]();
} catch (IOException e) {
// TODO Auto-generated catch block
[Link]();
}
}
}
GUI
import [Link];
import [Link];
import [Link];
public class myFrame extends JFrame{
myFrame(){
[Link](true);
[Link]("ayaeljahidi");
[Link](420, 420);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](false);
ImageIcon image = new ImageIcon("/home/aya/Desktop/[Link]");
[Link]([Link]());
[Link]().setBackground(new Color(255,255,255)); //123,50,250
public class Main {
public static void main(String[] args) {
new myFrame();
}
}
fd
edfz