Loading repository data…
Loading repository data…
lironmiz / repository
Designed for saving assignments, submission exercises and projects
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
Intended for saving solutions for tests , exercises and assignments as part of an introductory course to computer science in the Java language 😎
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_ '-._ @@
@@(___.' '.___) @@
+@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @+
// 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
*/
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 |
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
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() |
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
In Java, arithmetic operations are used to perform mathematical calculations on variables.
| Operations | Symbol |
|---|---|
| Addition | + |
| Subtraction | - |
| Multiplication | * |
| Division | / |
| Modulus | % |
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
| Shorthand Expressions | Symbol |
|---|---|
| Addition Assignment | += |
| Subtraction Assignment | -= |
| Multiplication Assignment | *= |
| Division Assignment | /= |
| Modulus Assignment | %= |
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
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 t |