
// Conditional Statements - If else
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner myInput = new Scanner(System.in);
// First number input
System.out.print("Enter first number");
int fnum = myInput.nextInt();
// Second number input
System.out.print("Enter second number");
int snum = myInput.nextInt();
// Operation input
System.out.print("Enter the operation");
char op = myInput.next().charAt(0);
//My calculator
if(op == '+') {☻
System.out.print(fnum+snum);
}else if (op == '-') {
System.out.print(fnum-snum);
}else {
System.out.print("Operation not found");
}
}
// Conditional Statements - If else -Calculator
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner myInput = new Scanner(System.in);
// First number input
System.out.print("Enter first number");
int fnum = myInput.nextInt();
// Second number input
System.out.print("Enter second number");
int snum = myInput.nextInt();
// Operation input
System.out.print("Enter the operation");
char op = myInput.next().charAt(0);
//My calculator
if(op == '+') {
System.out.print(fnum+snum);
}else if (op == '-') {
System.out.print(fnum-snum);
}else if (op == '*') {
System.out.print(fnum*snum);}
else if (op == '/') {
System.out.print(fnum/snum);}
else {
System.out.print("Operation no found");
}
}
}
// Conditional Statements - Switch
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner myInput = new Scanner(System.in);
int day =1;
switch(day) {
case 1 :
System.out.println("Saturday");
break;
case 2 :
System.out.println("Sunday");
break;
case 3 :
System.out.println("Monday");
break;
default:// Will excute if all cases are not found
System.out.print("Day not found");
}
}
}
// Calculator
char operator;
Double number1, number2, result;
// create an object of Scanner class
Scanner input = new Scanner(System.in);
// ask users to enter operator
System.out.println("Choose an operator: +, -, *, or /");
operator = input.next().charAt(0);
// ask users to enter numbers
System.out.println("Enter first number");
number1 = input.nextDouble();
System.out.println("Enter second number");
number2 = input.nextDouble();
switch (operator) {
// performs addition between numbers
case '+':
result = number1 + number2;
System.out.println(number1 + " + " + number2 + " = " + result);
break;
// performs subtraction between numbers
case '-':
result = number1 - number2;
System.out.println(number1 + " - " + number2 + " = " + result);
break;
// performs multiplication between numbers
case '*':
result = number1 * number2;
System.out.println(number1 + " * " + number2 + " = " + result);
break;
// performs division between numbers
case '/':
result = number1 / number2;
System.out.println(number1 + " / " + number2 + " = " + result);
break;
default:
System.out.println("Invalid operator!");
break;
}
// Week Days
Scanner myInput = new Scanner(System.in);
// Week days program
System.out.print("Enter the week number: ");
int day = myInput.nextInt();
switch(day) {
case 1 :
System.out.println("Saturday");
break;
case 2 :
System.out.println("Sunday");
break;
case 3 :
System.out.println("Monday");
break;
case 4 :
System.out.println("Tuesday");
break;
case 5 :
System.out.println("Wensday");
break;
case 6 :
System.out.println("Thursday");
break;
case 7 :
System.out.println("Friday");
break;
default:// Will excute if all cases are not found
System.out.print("Day not found");
}
Scanner myInput = new Scanner(System.in);
//syntax
for(int i=1;i<=5;i++) {
System.out.println("Hello world");
}
//syntax
//1-initial vlaue , 2 - condition,3-increment,4-statements
int i = 5;
while(i>=1) {
//statements
System.out.println(i);
//3- increment
i--;
}
for(int i=1 ;i<=5;i++) {
if(i==3) {
continue;
}
System.out.println(i);
}
// Nested for loop
// Outer loop
// A month project
for(int i = 1;i<=4;i++) {
System.out.println("Week "+i);
// inner loop
for(int j = 1;j<=7;j++) {
System.out.println("Day "+j);
}
}
// Syntax
// index 0 1 2
String[] names = {"Ahmed","Mohammed","Adel"};
System.out.println(names[2]);
}
String[] names = {"Ahmed","Mohammed"};//1darray
// 2 d array -> arrays in array
int[][] TwoDarray={
{16,316,17,19},
{34,12,16,37},
{36,24,11,10,}
};
System.out.println(TwoDarray[1][4]);
}
/*
0 1 2 3 4 5 6
0 16
1 37
2
3
4
* */
int[] numbers= {16,13,16,18,149,16,793,149};
// syntax - foreach loop
for(int num : numbers) {
System.out.println(num);
}
معلومة مهمه جدا أن
الدوال بتكتب بره ال Main method
- All methods are written out of the main methods , so be sure to write it out so you don't get an error
// Syntax
// Doesn't return value
public static void printHello() // Method Header
{
System.out.println("Hello world");
}
}
String name = printNumber();
System.out.println(name);
}
// Syntax
// return method
// Return type must be the same type as the method
public static String printNumber() // Method Header
{
return "Hello";
}
double first = multiMethod(5,5);
System.out.println(first);
}
public static double multiMethod(double num1,double num2) {
return num1*num2;
}
Scanner myInput = new Scanner(System.in);
System.out.println(sum(100));
// Recursion - Factorial ☻
}
public static int sum(int num) {
if(num > 0) {
return num+sum(num-1);// 5+4+3+2+1
}else {
return 0;
}
}
System.out.println(x);
// All code before variable x cannot use it
int x = 1 ;
// Code under variable x can use it
System.out.println(x);
}// end of main method block
من المهم جدا فهم البرمجة الشيئية لفوائدها العديدة مثل
* عدم التكرار
* الترتيب والاحترافية
* تقليل الكتابة
* التنظيم
* وغيره كثير جدا
// Object Oriented Programming (OOP)- Classes and Objects
import java.util.Scanner;
public class Main {// Cars - Toyota - mercedes
// Makes program faster and fast to excute
// Write less
// [DRY] Don't repeat yourself
int x = 5;
public static void main(String[] args) {
Scanner myInput = new Scanner(System.in);
// Syntax for object
Main myObject = new Main();
System.out.println(myObject.x);
}
}
// Classes Attributes - fields = Variables of the class
import java.util.Scanner;
public class Main {
int x ;// Field - By default is zero
int y = 3;// field
public static void main(String[] args) {
Scanner myInput = new Scanner(System.in);
Main myObject = new Main();
System.out.println(myObject.x);
}
}
// Classes Methods - Diffrent between Public and static
import java.util.Scanner;
public class Main {
// Public and non static at the same class
public void printHello() {
System.out.println("Hello world");
}
static void printName(String name) {
System.out.println("Your name is "+name);
}
public static void main(String[] args) {
Scanner myInput = new Scanner(System.in);
//static method can be called at the same class without the object
printName("Mohammed");
Main myObject = new Main();
myObject.printHello();
}
}
// Classes Constructors- Special Methods
import java.util.Scanner;
/*Note that the constructor name must match the class name,
* and it cannot have a return type (like void).*/
public class Main {
int x ;// field
int carYear;
String carName;
public Main(int y) {
x = y;
}
public Main(int year , String name) {
carYear = year;
carName = name;
}
// Predifined constructor
// default construcotor
public Main() {
}
public static void main(String[] args) {
Scanner myInput = new Scanner(System.in);
Main myObject = new Main();
Main myCar = new Main(1990,"Mercedes");
System.out.println(myCar.carName);
System.out.println(myCar.carYear);
}
}
// Access Modifiers - Public -Private - Default
import java.util.Scanner;
/*public The code is accessible for all classes
private The code is only accessible within the declared class
default The code is only accessible in the same package*/
public class Main {
int x = 4;// Attribute
private int y = 6;
public static void main(String[] args) {
Scanner myInput = new Scanner(System.in);
Main myObject = new Main();
System.out.println(myObject.y);
}
}
// Encapsulation - getters and Setters
import java.util.Scanner;
/*The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must:
1-declare class variables/attributes as private
2-provide public get and set methods to access and update the value of a private variable*/
public class Main {
public static void main(String[] args) {
Scanner myInput = new Scanner(System.in);
Student s = new Student();
s.setName("Ahmed");
s.setAge(15);
System.out.println(s.getName());
System.out.println(s.getAge());
}
/*Why Encapsulation?
*
Better control of class attributes and methods
Class attributes can be made read-only (if you only use the get method), or write-only (if you only use the set method)
Flexible: the programmer can change one part of the code without affecting other parts
Increased security of data*/
}
// Inheritance - Subclass(child) - Superclass(Parent)
// Class car is the Child class or the subclass
class Car extends Mercedes{
int carModel = 2003;
public static void main(String[] args) {
Car myCar = new Car();
System.out.println(myCar.carModel);
System.out.println(myCar.model);
System.out.println(myCar.name);
Mercedes myMercedes = new Mercedes();
System.out.println(myMercedes.model);
myMercedes.maxSpeed();
Car.maxSpeed();
}
}
// this class is the parent or the superclass
class Mercedes {
int model = 2002;
String name = "Mercedes";
// If its static can be inherited to other classes
public static void maxSpeed() {
System.out.println("Max speed is 300km/h");
}
}
// You cant do multiple inheritance but with interfaces you can use the mullti inheritance
// Polymorphism -> many forms - > reuseability ( Method overloading)
class Car extends Toyota {
public static void maxSpeed() {
System.out.println("Max speed is 100km/h");
}
public static void main(String[] args) {
Car myCar = new Car();
myCar.maxSpeed();
Mercedes myC = new Mercedes();
myC.maxSpeed();
Toyota myT = new Toyota();
myT.maxSpeed();
}
}
// this class is the parent or the superclass
class Mercedes extends Toyota {
public static void maxSpeed() {
System.out.println("Max speed is 300km/h");
}
}
public class Toyota {
public static void maxSpeed() {
System.out.println("Max speed is 200 km/h");
}
}
Why And When To Use Abstract Classes and Methods?
To achieve security - hide certain details and only show the important details of an object.
Notes on Interfaces:
Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an "Animal" object in the MyMainClass)
Interface methods do not have a body - the body is provided by the "implement" class
On implementation of an interface, you must override all of its methods
Interface methods are by default abstract and public
Interface attributes are by default public, static and final
An interface cannot contain a constructor (as it cannot be used to create objects)
Why And When To Use Interfaces?
1) To achieve security - hide certain details and only show the important details of an object (interface).
2) Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).
// Exceptions
public class Main {
public static void main(String[] args) {
// Syntax
/*try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}*/
try {
int[] myNums= {3,16,15};
System.out.println(myNums[5]);}
catch(Exception e){
System.out.println("This will run if there is error in try");
}
finally {
System.out.println("Finally block");
}
}
}
*أهلا وسهلا بكم في كورس الجافا من الصفر ستتعلم الكثير عن البرمجة وكيفية كتابة برامج كثيرة بلغة الجافا*
* في حالة لم تقدر أن تشتري الكورس يمكنك التواصل معي على الخاص *
*نبذة عن لغة الجافا*
ما هي جافا؟
جافا أو Java هي لغة برمجة عالية المستوى وكائنية التوجه ومتعددة الاستعمالات. لغة برمجة عالية المستوى high-level حيث تتميز بسهولتها واستخدامها لمصطلحات إنجليزية مفهومة وتخفي الكثير من التفاصيل للتعامل مع عتاد الحاسب بعكس اللغات منخفضة المستوى. لغة برمجة كائنية التوجه (object-oriented (OO حيث أنه يتم تمثيل كل شيء على هئية كائن، وكل كائن له نوع معين وصفات وأفعال تميزه عن غيره. لغة برمجة متعددة الاستعمالات general-purpose حيث أنها تستخدم لصناعة برمجيات في شتى المجالات، ومنها على سبيل المثال لا الحصر: تطبيقات سطح المكتب، تطبيقات الويب، تطبيقات الهواتف المحمولة، تطبيقات الأجهزة محدودة الموارد embedded systems، وغيرها.
جافا هي أيضاً منصة برمجية software platform تحتوي على آلة جافا افتراضية (Java Virtual Machine (JVM تقوم بتشغيل برامج الجافا عليها بغض النظر عن نظام التشغيل (operating system (OS أو نوع ومعمارية المعالج CPU architecture التي تشغل هذه الآلة. تتألف برامج الجافا التي تقوم بتشغيلها آلة الجافا الافتراضية من لغة وسيطة intermediate language تسمى جافا بايت كود Java bytecode. يتم إنتاج الجافا بايت كود عادةً بواسطة مترجم الجافا Java compiler وهو الذي يقوم بترجمة لغة الجافا إلى لغة الجافا بايت كود التي تفهمها آلة الجافا الافتراضية. يوجد أيضاً العديد من لغات البرمجة التي يمكن ترجمتها إلى جافا بايت كود، وتسمى هذه اللغات بلغات آلة الجافا الافتراضية JVM languages، ومن أشهرها لغة سكالا Scala.
بسبب طبيعة آلة جافا الافتراضية من حيث أنها تقوم بتشغيل نفس الجافا بايت كود على أي بيئة تشغيلية، تتميز لغة الجافا بمبدأ “أكتب مرة، شغل بأي مكان” (Write Once, Run Anywhere” (WORA”. بمعنى آخر، أكتب برنامج جافا مرة واحدة فقط، وسوف يعمل على جميع الأنظمة التشغيلية، بعكس اللغات الأخرى مثل ++C\C حيث يتم كتابة برنامج مختلف لكل نظام تشغيل.
**
للأسلة أرجو التواصل معي على الخاص **