💻 Computer-Science-in-Java 💻
Intended for saving solutions for tests , exercises and assignments as part of an introductory course to computer science in the Java language 😎
Useful link
:star2: Course Material
Introduction And Basics Of The Language
Object Oriented Programming
Flow Control (conditional sentences and loops)
Arrays
Extending Object-Oriented-Programming - inheritance, static methods and variables, loading of methods, cases, polymorphism and interfaces
Complications
Search And Sort Algorithms
Recursion
Linked Lists
Stacks And Queues
Trees And Binary Trees
Computational
+@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @+
@@ o o @@
@@ | | @@
@@ _L_L_ @@
@@ ❮\/__-__\/❯ Programming isn't about what you know @@
@@ ❮(|~o.o~|)❯ It's about what you can figure out @@
@@ ❮/ \`-'/ \❯ @@
@@ _/`U'\_ @@
@@ ( . . ) .----------------------------. @@
@@ / / \ \ | while( ! (succed=try() ) ) | @@
@@ \ | , | / '----------------------------' @@
@@ \|=====|/ @@
@@ |_.^._| @@
@@ | |"| | @@
@@ ( ) ( ) Testing leads to failure @@
@@ |_| |_| and failure leads to understanding @@
@@ _.-' _j L_ '-._ @@
@@(___.' '.___) @@
+@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @+
🤩 Computer-Science-in-Java-Summary 🤩
1. TABLE OF CONTENTS
// headers
//*******************************************************
// Exam in July 2022B (91)
// Semester 2022B
// Solutions to the test
// Author: liron mizrahi
//*******************************************************
//*******************************************************
// IntList.java
// the class reprsents IntList
// Author: liron mizrahi
//*******************************************************
// API
/**
* method return true if the arr values shift by 1
* @param: intp[ arr1, int[] arr2
* @return: boolean
*/
2. PRIMITIVE VARIABLES TYPES
In Java, variables are used to store and manipulate data. There are several types of variables, each with its own characteristics and uses.
| Definition |
Example |
Size |
Range |
| char |
'b', 'B', '9', '&' |
1 byte |
-2^7...2^7-1 (-128...127) |
| int |
-5, 9, 8214 |
4 bytes |
-2^31...2^31-1 |
| long |
-3, 77, 8234 |
8 bytes |
-2^63...2^63-1 |
| short |
-1, 3, 1456 |
2 bytes |
-2^15...2^15-1 |
| byte |
-2, 8, 42 |
1 byte |
-2^7...2^7-1 |
| double |
5.22, -89, 1.65 |
8 bytes |
-1.710^308 to 1.710^308 |
| float |
44.22, -89, 8.6 |
4 bytes |
-3.410^38 to 3.410^38 |
| boolean |
true / false |
1 byte |
true / false |
2.1 Example
public class ExamplesOfBasicVariablesTypes
{
public static void main(String[] args)
{
// define char
char c = 'k';
// define int
int num = 5;
// define long
long num1 = 765;
// define sort
sort num2 = 6;
// define byte
byte num3 = 32;
// define double
double num4 = 326;
// define float
float num5 = 89;
// define boolean
boolean bool = true;
}// end of method main
}// end of class ExamplesOfBasicVariablesTypes
3. TAKING INPUT
in this table we use:
Scanner scan = new Scanner(System.in)
In Java, there are several ways to take input from a user.
Here the Scanner method:
(Remember to use the Scanner class you need to import it using import java.util.Scanner;
It should be used at the very beginning of your java file before any other code written.
So, you can use the methods in the table below after importing the class.)
| Data Type |
Format |
| int |
in.nextInt() |
| short |
in.nextShort() |
| long |
in.nextLong() |
| char |
in.next().charAt(0) |
| float |
in.nextFloat() |
| double |
in.nextDouble() |
| byte |
in.nextByte() |
| boolean |
in.nextBoolean() |
| String |
in.next() |
| A string across an entire line including spaces |
in.nextLine() |
3.1 Example
import java.util.Scanner;
public class InputExample
{
public static void main(String[] args)
{
// make a Scanner object
Scanner scan = new Scanner(System.in);
// Prompt user to enter an integer
System.out.print("Enter an integer: ");
int num1 = scan.nextInt();
// Prompt user to enter a short
System.out.print("Enter a short: ");
short num2 = scan.nextShort();
// Prompt user to enter a long
System.out.print("Enter a long: ");
long num3 = scan.nextLong();
// Prompt user to enter a character
System.out.print("Enter a character: ");
char ch = scan.next().charAt(0);
// Prompt user to enter a float
System.out.print("Enter a float: ");
float num4 = scan.nextFloat();
// Prompt user to enter a double
System.out.print("Enter a double: ");
double num5 = scan.nextDouble();
// Prompt user to enter a byte
System.out.print("Enter a byte: ");
byte num6 = scan.nextByte();
// Prompt user to enter a boolean
System.out.print("Enter a boolean: ");
boolean bool = scan.nextBoolean();
System.out.println("You entered: " + num1 + ", " + num2 + ", " + num3 + ", " + ch + ", " + num4 + ", " + num5 + ", " + num6 + ", " + bool);
}// end of method main
}// end of class InputExample
4. ARITHMETIC
In Java, arithmetic operations are used to perform mathematical calculations on variables.
| Operations |
Symbol |
| Addition |
+ |
| Subtraction |
- |
| Multiplication |
* |
| Division |
/ |
| Modulus |
% |
4.1 Example
public class Arithmetic
{
public static void main(String[] args)
{
int a = 5;
int b = 2;
// addition
int c = a + b;
System.out.println("Addition: " + a + " + " + b + " = " + c);
// subtraction
c = a - b;
System.out.println("Subtraction: " + a + " - " + b + " = " + c);
// multiplication
c = a * b;
System.out.println("Multiplication: " + a + " * " + b + " = " + c);
// division
c = a / b;
System.out.println("Division: " + a + " / " + b + " = " + c);
// modulus
c = a % b;
System.out.println("Modulus: " + a + " % " + b + " = " + c);
}// end of method main
}// end of class Arithmetic
4.2 Shorthand Experssions
| Shorthand Expressions |
Symbol |
| Addition Assignment |
+= |
| Subtraction Assignment |
-= |
| Multiplication Assignment |
*= |
| Division Assignment |
/= |
| Modulus Assignment |
%= |
4.3 Shorthand Experssions Example
public class ShorthandExpressions
{
public static void main(String[] args)
{
int x = 10;
int y = 5;
// addition assignment
x += y;
System.out.println("Addition Assignment: x += y : " + x);
// subtraction assignment
x -= y;
System.out.println("Subtraction Assignment: x -= y : " + x);
// multiplication assignment
x *= y;
System.out.println("Multiplication Assignment: x *= y : " + x);
// division assignment
x /= y;
System.out.println("Division Assignment: x /= y : " + x);
// modulus assignment
x %= y;
System.out.println("Modulus Assignment: x %= y : " + x);
}// end of method main
}// end of class ShorthandExpressions
5. CASTING
In Java, casting is the process of converting one data type to another.
| Type |
Description |
| Implicit casting |
Also known as automatic casting, it occurs when a smaller type is converted to a larger type without the need for explicit casting. |
| Explicit casting |
Also known as manual casting, it occurs when a larger type is converted to a smaller type. |
5.1 Example
public class Casting
{
public static void main(String[] args)
{
// Implicit casting
int a = 10;
long b = a; // automatic casting from int to long
// Explicit casting
double c = 2.12;
int d = (int)c; // explicit casting from double to int
System.out.println("Implicit casting: int to long : " + b);
System.out.println("Explicit casting: double to int : " + d);
}// end of method main
}// end of class Casting
6. RELATIONAL AND LOGICAL EXPRESSIONS
In Java, relational and logical expressions are used to make comparisons and control the flow of a program.
| Logical And Relational Expressions |
Symbol |
Description |
| AND |
&& |
Returns true if both operands are true |
| OR |
|| |
Returns true if one or both operands are true |
| NOT |
! |
Returns true if the operand is false and false if the operand is true |
| Less than |
< |
Returns true if the left operand is less than the right operand |
| Less than or equal to |
<= |
Returns true if the left operand is less than or equal to the right operand |