إمثلة إضافية
1-دالة لحساب المساحة بأشكال متعددة
public class AreaCalculator {
// دالة لحساب مساحة المستطيل (الطول × العرض)
public int calculateArea(int length, int width) {
return length * width;
}
// دالة لحساب مساحة المربع (الضلع × الضلع)
public int calculateArea(int side) {
return side * side;
}
// دالة لحساب مساحة الدائرة (π × نصف القطر²)
public double calculateArea(double radius) {
return Math.PI * radius * radius;
}
public static void main(String[] args) {
AreaCalculator calculator = new AreaCalculator();
System.out.println("مساحة المستطيل: " + calculator.calculateArea(10, 5)); // الناتج: 50
System.out.println("مساحة المربع: " + calculator.calculateArea(7)); // الناتج: 49
System.out.println("مساحة الدائرة: " + calculator.calculateArea(3.5)); // الناتج: 38.48451000647496
}
}هنا لدينا ثلاث دوال بنفس الاسم calculateArea:
- الدالة الأولى تحسب مساحة المستطيل باستخدام الطول والعرض.
- الدالة الثانية تحسب مساحة المربع باستخدام طول الضلع.
- الدالة الثالثة تحسب مساحة الدائرة باستخدام نصف القطر، مع الأخذ في الاعتبار أن قيمة نصف القطر من النوع
double، مما يجعلها مختلفة عن الدوال الأخرى..
.
2- دالة لحساب مجموع أعداد صحيحة ومصفوفات
public class SumCalculator {
// دالة لحساب مجموع عددين صحيحين
public int sum(int a, int b) {
return a + b;
}
// دالة لحساب مجموع ثلاثة أعداد صحيحة
public int sum(int a, int b, int c) {
return a + b + c;
}
// دالة لحساب مجموع العناصر في مصفوفة
public int sum(int[] numbers) {
int total = 0;
for (int num : numbers) {
total += num;
}
return total;
}
public static void main(String[] args) {
SumCalculator calculator = new SumCalculator();
System.out.println("مجموع عددين: " + calculator.sum(10, 20)); // الناتج: 30
System.out.println("مجموع ثلاثة أعداد: " + calculator.sum(10, 20, 30)); // الناتج: 60
System.out.println("مجموع مصفوفة: " + calculator.sum(new int[]{1, 2, 3, 4, 5})); // الناتج: 15
}
}هنا لدينا ثلاث دوال
sum:- الدالة الأولى تأخذ عددين صحيحين.
- الدالة الثانية تأخذ ثلاثة أعداد صحيحة.
- الدالة الثالثة تأخذ مصفوفة من الأعداد الصحيحة وتحسب مجموع عناصرها.
مثال اخر
3- دالة للعثور على أكبر قيمة بين رقمين أو ثلاثة أرقام
.
public class MaxFinder {
// دالة لإيجاد القيمة الكبرى بين عددين صحيحين
public int max(int a, int b) {
return (a > b) ? a : b;
}
// دالة لإيجاد القيمة الكبرى بين ثلاثة أعداد صحيحة
public int max(int a, int b, int c) {
return max(max(a, b), c);
}
// دالة لإيجاد القيمة الكبرى بين عددين عشريين
public double max(double a, double b) {
return (a > b) ? a : b;
}
public static void main(String[] args) {
MaxFinder finder = new MaxFinder();
System.out.println("القيمة الكبرى بين 5 و 10: " + finder.max(5, 10)); // الناتج: 10
System.out.println("القيمة الكبرى بين 5 و 10 و 15: " + finder.max(5, 10, 15)); // الناتج: 15
System.out.println("القيمة الكبرى بين 7.5 و 2.3: " + finder.max(7.5, 2.3)); // الناتج: 7.5
}
}
هنا لدينا ثلاث دوال max:
- الدالة الأولى تقارن بين عددين صحيحين.
- الدالة الثانية تقارن بين ثلاثة أعداد صحيحة باستخدام الدالة الأولى كجزء من تنفيذها.
- الدالة الثالثة تقارن بين عددين عشريين.
ما هو Constructor؟.
الـ Constructor هو دالة خاصة تُستخدم لتهيئة (initialize) الكائنات (objects) عند إنشائها. يتم استدعاؤه عند إنشاء كائن جديد باستخدام الكلمة المفتاحية new. وظيفة الـ Constructor الأساسية هي تعيين القيم الأولية لأعضاء البيانات (attributes) في الكائن.
خصائص Constructor:
نفس اسم الصف (Class): يجب أن يكون اسم الـ Constructor هو نفس اسم الصف الذي يتم تعريفه بداخله.
لا يوجد نوع إرجاع (Return Type): لا يحتوي الـ Constructor على نوع إرجاع حتى void.
يُستدعى مرة واحدة فقط عند إنشاء الكائن.
أنواع Constructors:
Constructor الافتراضي (Default Constructor):
هو Constructor بدون معلمات، يتم إنشاؤه تلقائيًا إذا لم يتم تعريف أي Constructor في الصف. يعطي قيمًا افتراضية للبيانات مثل 0 أو null
public class Person {
private String name;
private int age;
// Constructor الافتراضي
public Person() {
name = "Unknown";
age = 0;
}
public void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
// استدعاء Constructor الافتراضي
person.display();// Output:
Name: Unknown, Age: 0
}
}
2-Constructor المُعَدل (Parameterized Constructor):
هو Constructor يحتوي على معلمات، يُستخدم لتعيين القيم للأعضاء بناءً على المعطيات المقدمة عند إنشاء الكائن.
مثال:
public class Car {
private String model;
private int year;
// Constructor مع معلمات
public Car(String model, int year) {
this.model = model;
this.year = year;
}
public void display() {
System.out.println("Model: " + model + ", Year: " + year);
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car("Toyota", 2021); // استدعاء Constructor مع معلمات
car.display();
// Output: Model: Toyota, Year: 2021
}
}3-Constructor
النسخ (Copy Constructor):
يُستخدم لنسخ قيم كائن إلى كائن آخر من نفس النوع.
مثال:
public class Person {
private String name;
private int age;
// Constructor مع معلمات
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Constructor النسخ
public Person(Person other) {
this.name = other.name;
this.age = other.age;
}
public void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person original = new Person("Ali", 30);
Person copy = new Person(original); // استدعاء Constructor النسخ
copy.display(); // Output: Name: Ali, Age: 30
}
}Constructor Chaining
يتم استدعاء عدة Constructors من نفس الصف أو من صف الأب (parent class) باستخدام this() لاستدعاء Constructors داخل نفس الصف، أو super() لاستدعاء Constructors في الصف الأب.
مثال :
public class Employee {
// Constructor الافتراضي
Employee() {
this(2023); // استدعاء Constructor مع معلمة واحدة
System.out.println("In Default Constructor Of Employee Class");
}
// Constructor مع معلمة واحدة
Employee(int id) {
this("Ankush"); // استدعاء Constructor مع معلمة نصية
System.out.println("In Parameterized Constructor with Integer Parameter: " + id);
}
// Constructor مع معلمة نصية
Employee(String name) {
System.out.println("In Parameterized Constructor With String Parameter: " + name);
}
public static void main(String[] args) {
Employee emp = new Employee();
}
}الإخراج:
mathematica
In Parameterized Constructor With String Parameter: Ankushيُمكن تحميل Constructors بنفس اسم الصف مع عدد مختلف أو نوع مختلف من المعلمات.
In Parameterized Constructor with Integer Parameter: 2023
In Default Constructor Of Employee Class
Constructor Overloading
مثال:
public class Student {
private String name;
private int age;
// Constructor الافتراضي
public Student() {
name = "Unknown";
age = 0;
}
// Constructor مع معلمة واحدة
public Student(String name) {
this.name = name;
age = 0;
}
// Constructor مع معلمتين
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}اعتقد كافيي بس جان ضروري انزلهن لان هذا ماده الامتحان مال باجر
اذا سمحت فرصه الي بعشوي انزل امثله اكثر
هذا فقط امثله الملزمه
💚🌿
اذا غلطت بشي نبهوني لان تركيزي حاليا بالسالب
💋2❤1🔥1🥰1🌚1
سؤال المختبر القادم للهياكل علوم صباحي /
//
اكتب برنامج يقرء مصفوفه احاديه ومصفوفة ثنائيه ومصفوفه ثلاثيه ومصفوفه مثلثه تكون 4*4
وتكون بيها عمليات خاصه للجزء العلوي عمليات خاصه للجزء السفلي
ثم يطلب من المستخدم اختيار نوع المصفوفه ووحجم المصفوفه وعناصر المجموعه ثم طباعتها واجراء 5 عمليات عليها ويبقى يطلب من المستخدم اجراء عمليات لحين طلب المستخدم الخروج من العمليات
🤯5🔥1
JAVA OOP
سؤال المختبر القادم للهياكل علوم صباحي / // اكتب برنامج يقرء مصفوفه احاديه ومصفوفة ثنائيه ومصفوفه ثلاثيه ومصفوفه مثلثه تكون 4*4 وتكون بيها عمليات خاصه للجزء العلوي عمليات خاصه للجزء السفلي ثم يطلب من المستخدم اختيار نوع المصفوفه ووحجم المصفوفه وعناصر…
الكود موجود بس انتضر موافقه السلطات العليا علمود انزله😂😂
لا بس اتاكد منه اكثر وانزله
تقريبا صار
450 سطر
لا بس اتاكد منه اكثر وانزله
تقريبا صار
450 سطر
😱6🔥2🗿2👎1🤣1😭1
JAVA OOP
سؤال المختبر القادم للهياكل علوم صباحي / // اكتب برنامج يقرء مصفوفه احاديه ومصفوفة ثنائيه ومصفوفه ثلاثيه ومصفوفه مثلثه تكون 4*4 وتكون بيها عمليات خاصه للجزء العلوي عمليات خاصه للجزء السفلي ثم يطلب من المستخدم اختيار نوع المصفوفه ووحجم المصفوفه وعناصر…
import java.util.Scanner;
public class hmd{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("اختر نوع المصفوفة: 1. أحادية 2. ثنائية 3. ثلاثية 4. مثلثية 5. خروج");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println(" enter size :");
int size = scanner.nextInt();
int[] array = new int[size];
System.out.println("أدخل العناصر:");
for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}
printArray(array);
arrayD1(array);
break;
case 2:
System.out.println(" enter r:");
int rows = scanner.nextInt();
System.out.println(" enter c:");
int cols = scanner.nextInt();
int[][] matrix = new int[rows][cols];
System.out.println("أدخل العناصر:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = scanner.nextInt();
}
}
printArray(matrix);
arrayD2(matrix);
break;
case 3:
System.out.println(" enter l :");
int layers = scanner.nextInt();
System.out.println(" enter r :");
int threesRows = scanner.nextInt();
System.out.println(" enter c :");
int threesCols = scanner.nextInt();
int[][][] threeDMatrix = new int[layers][threesRows][threesCols];
System.out.println("أدخل العناصر:");
for (int i = 0; i < layers; i++) {
for (int j = 0; j < threesRows; j++) {
for (int k = 0; k < threesCols; k++) {
threeDMatrix[i][j][k] = scanner.nextInt();
}
}
}
printArray(threeDMatrix);
arrayD3(threeDMatrix);
break;
case 4:
int[][] triangularMatrix = new int[4][4];
System.out.println("اختر الجزء: 1. علوي 2. سفلي");
int part = scanner.nextInt();
if (part == 1) {
System.out.println("أدخل العناصر للجزء العلوي:");
for (int i = 0; i < 4; i++) {
for (int j = i; j < 4; j++) {
triangularMatrix[i][j] = scanner.nextInt();
}
}
} else if (part == 2) {
System.out.println("أدخل العناصر للجزء السفلي:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j <= i; j++) {
triangularMatrix[i][j] = scanner.nextInt();
}
}
} else {
System.out.println("اختيار غير صالح.");
break;
}
printArray(triangularMatrix);
arrayTriangular(triangularMatrix);
break;
case 5:
exit = true;
System.out.println("تم الخروج من البرنامج.");
break;
default:
System.out.println("اختيار غير صالح.");
}
}
}
JAVA OOP
سؤال المختبر القادم للهياكل علوم صباحي / // اكتب برنامج يقرء مصفوفه احاديه ومصفوفة ثنائيه ومصفوفه ثلاثيه ومصفوفه مثلثه تكون 4*4 وتكون بيها عمليات خاصه للجزء العلوي عمليات خاصه للجزء السفلي ثم يطلب من المستخدم اختيار نوع المصفوفه ووحجم المصفوفه وعناصر…
private static void arrayD1(int[] array) {
boolean backToMenu = false;
while (!backToMenu) {
printArray(array);
System.out.println("اختر عملية:");
System.out.println("1. sum ");
System.out.println("2. avg ");
System.out.println("3. max ");
System.out.println("4. min ");
System.out.println("5. ضرب العناصر في 2");
System.out.println("6. العودة إلى القائمة الرئيسية");
int operation = new Scanner(System.in).nextInt();
switch (operation) {
case 1:
System.out.println(" sum : " + sum(array));
break;
case 2:
System.out.println(" avg : " + average(array));
break;
case 3:
System.out.println(" max : " + max(array));
break;
case 4:
System.out.println(" min : " + min(array));
break;
case 5:
multiplyByTwo(array);
break;
case 6:
backToMenu = true;
break;
default:
System.out.println("اختيار غير صالح!");
}
}
}
private static void arrayD2(int[][] matrix) {
boolean backToMenu = false;
while (!backToMenu) {
printArray(matrix);
System.out.println("اختر عملية:");
System.out.println("1. sum ");
System.out.println("2. avg ");
System.out.println("3. max ");
System.out.println("4. min ");
System.out.println("5. ضرب العناصر في 2");
System.out.println("6. العودة إلى القائمة الرئيسية");
int operation = new Scanner(System.in).nextInt();
switch (operation) {
case 1:
System.out.println(" sum : " + sum(matrix));
break;
case 2:
System.out.println(" avg : " + average(matrix));
break;
case 3:
System.out.println(" max : " + max(matrix));
break;
case 4:
System.out.println(" min : " + min(matrix));
break;
case 5:
multiplyByTwo(matrix);
break;
case 6:
backToMenu = true;
break;
default:
System.out.println("اختيار غير صالح!");
}
}
}
private static void arrayD3(int[][][] matrix) {
boolean backToMenu = false;
while (!backToMenu) {
printArray(matrix);
System.out.println("اختر عملية:");
System.out.println("1. sum ");
System.out.println("2. avg ");
System.out.println("3. max ");
System.out.println("4. min ");
System.out.println("5. ضرب العناصر في 2");
System.out.println("6. العودة إلى القائمة الرئيسية");
int operation = new Scanner(System.in).nextInt();
switch (operation) {
case 1:
System.out.println(" sum : " + sum(matrix));
break;
case 2:
System.out.println(" avg : " + average(matrix));
break;
case 3:
System.out.println(" max : " + max(matrix));
break;
case 4:
System.out.println(" min : " + min(matrix));
break;
case 5:
multiplyByTwo(matrix);
break;
case 6:
backToMenu = true;
break;
default:
System.out.println("اختيار غير صالح!");
}
}
}private static void arrayTriangular(int[][] matrix) {
boolean backToMenu = false;
while (!backToMenu) {
printArray(matrix);
System.out.println("اختر عملية:");
System.out.println("1. sum ");
System.out.println("2. avg ");
System.out.println("3. max ");
System.out.println("4. min ");
System.out.println("5. ضرب العناصر في 2");
System.out.println("6. العودة إلى القائمة الرئيسية");
int operation = new Scanner(System.in).nextInt();
switch (operation) {
case 1:
System.out.println(" sum : " + sum(matrix));
break;
case 2:
System.out.println(" avg : " + average(matrix));
break;
case 3:
System.out.println(" max : " + max(matrix));
break;
case 4:
System.out.println(" min : " + min(matrix));
break;
case 5:
multiplyByTwo(matrix);
break;
case 6:
backToMenu = true;
break;
default:
System.out.println("اختيار غير صالح!");
}
}
}
private static int sum(int[] array) {
int total = 0;
for (int i = 0; i < array.length; i++) {
total += array[i];
}
return total;
}
private static double average(int[] array) {
return (double) sum(array) / array.length;
}
private static int max(int[] array) {
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
private static int min(int[] array) {
int min = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
}
}
return min;
}
private static int sum(int[][] matrix) {
int total = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
total += matrix[i][j];
}
}
return total;
}
private static double average(int[][] matrix) {
int count = 0;
int total = sum(matrix);
for (int i = 0; i < matrix.length; i++) {
count += matrix[i].length;
}
return (double) total / count;
}
private static int max(int[][] matrix) {
int max = matrix[0][0];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
}
}
}
return max;
}
private static int min(int[][] matrix) {
int min = matrix[0][0];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] < min) {
min = matrix[i][j];
}
}
}
return min;
}
private static int sum(int[][][] matrix) {
int total = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
for (int k = 0; k < matrix[i][j].length; k++) {
total += matrix[i][j][k];
}
}
}
return total;
}
private static double average(int[][][] matrix) {
int count = 0;
int total = sum(matrix);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
count += matrix[i][j].length;
}
}
return (double) total / count;
}🔥1
private static int max(int[][][] matrix) {
int max = matrix[0][0][0];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
for (int k = 0; k < matrix[i][j].length; k++) {
if (matrix[i][j][k] > max) {
max = matrix[i][j][k];
}
}
}
}
return max;
}
private static int min(int[][][] matrix) {
int min = matrix[0][0][0];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
for (int k = 0; k < matrix[i][j].length; k++) {
if (matrix[i][j][k] < min) {
min = matrix[i][j][k];
}
}
}
}
return min;
}
private static void printArray(int[] array) {
System.out.print("المصفوفة: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
private static void printArray(int[][] matrix) {
System.out.println("المصفوفة:");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
private static void printArray(int[][][] matrix) {
System.out.println("المصفوفة:");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
for (int k = 0; k < matrix[i][j].length; k++) {
System.out.print(matrix[i][j][k] + " ");
}
System.out.println();
}
System.out.println();
}
}
private static void multiplyByTwo(int[] array) {
for (int i = 0; i < array.length; i++) {
array[i] *= 2;
}
printArray(array);
}
private static void multiplyByTwo(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] *= 2;
}
}
printArray(matrix);
}
private static void multiplyByTwo(int[][][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
for (int k = 0; k < matrix[i][j].length; k++) {
matrix[i][j][k] *= 2;
}
}
}
printArray(matrix);
}
}
JAVA OOP
سؤال المختبر القادم للهياكل علوم صباحي / // اكتب برنامج يقرء مصفوفه احاديه ومصفوفة ثنائيه ومصفوفه ثلاثيه ومصفوفه مثلثه تكون 4*4 وتكون بيها عمليات خاصه للجزء العلوي عمليات خاصه للجزء السفلي ثم يطلب من المستخدم اختيار نوع المصفوفه ووحجم المصفوفه وعناصر…
Telegram
JAVA OOP
import java.util.Scanner;
public class hmd{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("اختر نوع المصفوفة: 1. أحادية…
public class hmd{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("اختر نوع المصفوفة: 1. أحادية…
JAVA OOP
سؤال المختبر القادم للهياكل علوم صباحي / // اكتب برنامج يقرء مصفوفه احاديه ومصفوفة ثنائيه ومصفوفه ثلاثيه ومصفوفه مثلثه تكون 4*4 وتكون بيها عمليات خاصه للجزء العلوي عمليات خاصه للجزء السفلي ثم يطلب من المستخدم اختيار نوع المصفوفه ووحجم المصفوفه وعناصر…
وجان نخلص المصفوفات هذا اخر مختبر ناخذها
👏1
واجب الهياكل (مختبر) ,/علوم صباحي
البرنامج المطلوب:
١-قراءة خيط رمزي
٢-loop لكل الدوال المراد تطبيقها على الخيط الرمزي
٣- ادخال رقم الاختيار
٤-switch لتحديد العمليه المراد تنفيذها بناءا على الخيار المدخل
٥-من ضمن الخيارات خيار الخروج من البرنامج
البرنامج المطلوب:
١-قراءة خيط رمزي
٢-loop لكل الدوال المراد تطبيقها على الخيط الرمزي
٣- ادخال رقم الاختيار
٤-switch لتحديد العمليه المراد تنفيذها بناءا على الخيار المدخل
٥-من ضمن الخيارات خيار الخروج من البرنامج
👏1
import java.util.Locale;
import java.util.Scanner;
public class hmd2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
boolean running = true;
while (running) {
System.out.println("\nChoose an operation:");
System.out.println("1. Get a character at a specific index (charAt)");
System.out.println("2. Get the length of the string (length)");
System.out.println("3. Create a substring from a start index (substring)");
System.out.println("4. Create a substring from start to end indices (substring)");
System.out.println("5. Compare the string with another string (equals)");
System.out.println("6. Check if the string is empty (isEmpty)");
System.out.println("7. Concatenate strings (concat)");
System.out.println("8. Replace a character with another (replace)");
System.out.println("9. Split the string into words (split)");
System.out.println("10. Split the string into words with a limit (split with limit)");
System.out.println("11. Get the string from the constant pool (intern)");
System.out.println("12. Find the index of a character (indexOf)");
System.out.println("13. Find the index of a character from a specific position (indexOf with start index)");
System.out.println("14. Find the index of a substring (indexOf substring)");
System.out.println("15. Find the index of a substring from a specific position (indexOf substring with start index)");
System.out.println("16. Convert the string to lowercase (toLowerCase)");
System.out.println("17. Convert the string to lowercase with locale (toLowerCase with Locale)");
System.out.println("18. Convert the string to uppercase (toUpperCase)");
System.out.println("19. Convert the string to uppercase with locale (toUpperCase with Locale)");
System.out.println("20. Trim whitespace from the string (trim)");
System.out.println("21. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
JAVA OOP
واجب الهياكل (مختبر) ,/علوم صباحي البرنامج المطلوب: ١-قراءة خيط رمزي ٢-loop لكل الدوال المراد تطبيقها على الخيط الرمزي ٣- ادخال رقم الاختيار ٤-switch لتحديد العمليه المراد تنفيذها بناءا على الخيار المدخل ٥-من ضمن الخيارات خيار الخروج من البرنامج
switch (choice) {
case 1:
System.out.print("Enter the index: ");
int index = scanner.nextInt();
if (index >= 0 && index < input.length()) {
System.out.println("Character at index " + index + " is: " + input.charAt(index));
} else {
System.out.println("Invalid index.");
}
break;
case 2:
System.out.println("Length of the string: " + input.length());
break;
case 3:
System.out.print("Enter the starting index: ");
int start = scanner.nextInt();
if (start >= 0 && start < input.length()) {
System.out.println("Substring: " + input.substring(start));
} else {
System.out.println("Invalid start index.");}
break;
case 4:
System.out.print("Enter the starting index: ");
start = scanner.nextInt();
System.out.print("Enter the ending index (exclusive): ");
int end = scanner.nextInt();
if (start >= 0 && end <= input.length() && start < end) {
System.out.println("Substring: " + input.substring(start, end));
} else {
System.out.println("Invalid indices.");
}
break;
case 5:
System.out.print("Enter another string to compare: ");
String another = scanner.nextLine();
System.out.println("Strings are equal: " + input.equals(another));
break;
case 6:
System.out.println(input.isEmpty() ? "The string is empty." : "The string is not empty.");
break;
case 7:
System.out.print("Enter another string to concatenate: ");
String secondInput = scanner.nextLine();
System.out.println("Concatenated string: " + input.concat(secondInput));
break;
case 8:
System.out.print("Enter the character to replace: ");
char oldChar = scanner.next().charAt(0);
System.out.print("Enter the new character: ");
char newChar = scanner.next().charAt(0);
input = input.replace(oldChar, newChar);
System.out.println("Updated string: " + input);
break;
case 9:
String[] words = input.split(" ");
System.out.println("Words:");
for (String word : words) {
System.out.println(word);
}
break;
case 10:
System.out.print("Enter the maximum number of parts: ");
int limit = scanner.nextInt();
String[] parts = input.split(" ", limit);
System.out.println("Parts:");
for (String part : parts) {
System.out.println(part);
}
break;
case 11:
input = input.intern();
System.out.println("The string is now stored in the constant pool.");
break;
JAVA OOP
واجب الهياكل (مختبر) ,/علوم صباحي البرنامج المطلوب: ١-قراءة خيط رمزي ٢-loop لكل الدوال المراد تطبيقها على الخيط الرمزي ٣- ادخال رقم الاختيار ٤-switch لتحديد العمليه المراد تنفيذها بناءا على الخيار المدخل ٥-من ضمن الخيارات خيار الخروج من البرنامج
case 12:
System.out.print("Enter the character to search for: ");
char searchChar = scanner.next().charAt(0);
int position = input.indexOf(searchChar);
System.out.println(position != -1 ? "Found at position: " + position : "Character not found.");
break;
case 13:
System.out.print("Enter the character to search for: ");
searchChar = scanner.next().charAt(0);
System.out.print("Enter the starting index for the search: ");
int fromIndex = scanner.nextInt();
position = input.indexOf(searchChar, fromIndex);
System.out.println(position != -1 ? "Found at position: " + position : "Character not found.");
break;
case 14:
System.out.print("Enter the substring to search for: ");
String substring = scanner.nextLine();
position = input.indexOf(substring);
System.out.println(position != -1 ? "Found at position: " + position : "Substring not found.");
break;
case 15:
System.out.print("Enter the substring to search for: ");
substring = scanner.nextLine();
System.out.print("Enter the starting index for the search: ");
fromIndex = scanner.nextInt();
position = input.indexOf(substring, fromIndex);
System.out.println(position != -1 ? "Found at position: " + position : "Substring not found.");
break;
case 16:
System.out.println("String in lowercase: " + input.toLowerCase());
break;
case 17:
System.out.println("String in lowercase (with locale): " + input.toLowerCase(Locale.getDefault()));
break;
case 18:
System.out.println("String in uppercase: " + input.toUpperCase());
break;
case 19:
System.out.println("String in uppercase (with locale): " + input.toUpperCase(Locale.getDefault()));
break;
case 20:
System.out.println("Trimmed string: '" + input.trim() + "'");
break;
case 21:
System.out.println("Exiting the program...");
running = false;
break;
default:
System.out.println("Invalid choice. Please try again.");}}}}
🤣1