أمثلة وتمارين محلولة في لغة جافا
08 Feb 2020
إليك تمارين وأمثلة في البرمجة الموجهة في لغة البرمجة جافا مع الحلول.
الأسئلة جميعها مترابطة مع بعضها لإتمام كتابة وظائف في نفس البرنامج.
الحلول المطروحة هي حلول ممكنة وليست حلولًا مثاليّة.
الأسئلة
السؤال الأول
يتطلب كتابة برنامج لإدارة الطلاب حيث يتم حفظ الطلاب والمواد الدراسية ويقوم الطلاب لاحقًا بتسجيل أو إلغاء تسجيل أنفسهم في المواد.
البرنامج الرئيسي يكون في الملف: Main.java (انظر الكود أدناه)
والمطلوب هو:
-
كتابة أصناف (class) للطالب والجامعة: Student.java و University.java
-
حفظ هذه الأصناف في نفس مجلد Main.java
-
كتابة الوظائف المطلوبة أدناه في مكان العبارة TODO
في النهاية يجب أن يكون البرنامج قادرًا على إنشاء طلاب وحفظهم وعرضهم.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
University uni = new University();
// فكر هنا كيف يمكن إضافة الطلاب للجامعة
// هل يتطلب مصفوفة أم نوع بيانات أخرى؟
int operation = 0;
while (operation != 6) {
System.out.println("What Operation do you want to do?");
System.out.println("1- add Student");
System.out.println("2- view Students");
System.out.println("3- search Student");
System.out.println("4- edit Student info");
System.out.println("5- delete Student");
System.out.println("6- End Programm");
Scanner r = new Scanner(System.in);
operation = r.nextInt();
switch (operation) {
case 1:
/* TODO
أ) اطلب من المستخدم إدخال معلومات الطالب
ب) قم بإنشاء نموذج instance للصنف Student مع هذه المعلومات
ج) احفظ النموذج في برنامجنا نظام إدارة الطلاب
break;
case 2:
/* TODO
أ) قم بعرض رقم الطالب على الشاشة والاسم والكنية لكل الطلاب الذين تم حفظهم
*/
break;
}
}
}
}
السؤال الثاني
يتطلب تطوير البرنامج السابق بحيث:
أ. تطوير صنف الطالب Student وفق التالي:
-
إعادة كتابة دالة equals في صنف java.lang.Object (حيث ينبغي اعتبار عنصرين من صنف Student متساويين في حال كان رقم الطالب الخاص بهم متساو)
-
إعادة كتابة الدالة toString في صنف java.lang.Object ( الـ String الخاص بالعناصر من صنف Student يجب أن تكون على الشكل “رقم الطالب، الاسم الكنية” حيث تُكتب الكنية بأحرف كبيرة). حيث يتم استخدام دالة toString() عند استعراض الطلاب المخزنين في النظام.
ب. إجراء تغيير في صنف University بحيث يُحفظ الطلاب في شعاع java.util.Vector بدلًا من مصفوفة
ج. تطوير الصنف الرئيسي Main بحيث يمكن للمستخدم إضافة طلاب فقط في حالة عدم توافرهم أصلا في النظام. في حالة وجود الطالب مسبقًا وأراد المستخدم إضافته فستظهر رسالة خطأ توضح السبب.
عند كتابة البرنامج يرجى الانتباه للنقاط التالية:
-
يجب أن يحوي صنف Student على عشر متغيرات على الأقل
-
جميع المتغيرات في جميع الأصناف يجب أن تكون من نوع private وأن تحوي قيمة ابتدائية (غير الصفر أو null)
-
يجب أن يحوي الصنف Student على دالتي بناء (Constructors) على الأقل. ويجب عدم السماح بإنشاء عنصر طالب Student بدون رقم الطالب. ويمكن إنشاء عنصر طالب برقم طالب واسم وكنية.
-
يجب أن يحوي الصنف University على دالتي بناء على الأقل. يجب عدم السماح بإنشاء عنصر جامعة University بدون اسم الجامعة.
-
الانتباه لتنسيق الكود وبحيث يتم إزاحة الأسطر البرمجية في كل مجموعة { .. }
السؤال الثالث
لقد قمتَ بإعطاء المستخدم في البرنامج الرئيسي إمكانية إنشاء عناصر (object) جديدة من صنف الطالب Student وحفظها في عناصر من صنف الجامعة University class في حال عدم وجود تلك الطلاب في النظام مسبقًا.
مهامك الجديدة هي:
أ. تطوير الصنف الرئيسي Main بحيث يتم كتابة الوظائف المُعلّمة بـ TODO في الكود أدناه.
ب. إنشاء أصناف للمدرِّس Lecturer والمواد الدراسية Lessons
ج. ستلاحظ أن صنف المُدرِّس يشترك مع صنف الطالب في العديد من المتغيرات. استخدم مفهوم الوراثة وقم بإنشاء صنف شخص Person والذي يرث المتغيرات المشتركة من كلا الصنفين السابقين.
د. تطوير صنف الطالب بحيث يزوّد دالة ثابتة static للتأكد من رقم الطالب. يتم استدعاء هذه الدالة من الصنف الرئيسي Main لإظهار رسالة خطأ للمستخدم في حال أدخل رقمًا للطالب مخالفًا للقواعد التالية (أو أحدها):
-
يُسمح لرقم الطالب أن يحوي أرقامًا فقط.
-
يجب أن يكون طول رقم الطالب 7 أرقامًا.
-
لا يُسمح ﻷول رقم أن يكون صفرًا
هـ. إعادة كتابة صنف الجامعة University بحيث تستخدم خاصية generics لشعاع (Vector) الصنف بحيث يمكن حفظ العناصر من نفس الصنف فقط في عناصر الشعار Vector-Objects
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
University uni = new University();
int operation = 0;
while (operation != 16) {
System.out.println("What Operation do you want to do?");
System.out.println("1- add Student");
System.out.println("2- view Students");
System.out.println("3- search Student");
System.out.println("4- edit Student info");
System.out.println("5- delete Student");
System.out.println("6- add a Lecturer");
System.out.println("7- view the Lecturers ");
System.out.println("8- search Lecturer");
System.out.println("9- edit Lecturer info");
System.out.println("10- delete Lecturer");
System.out.println("11- add Lesson");
System.out.println("12- view the Lessons");
System.out.println("13- search Lesson");
System.out.println("14- edit Lesson info");
System.out.println("15- delete Lesson");
System.out.println("16- End Programm");
Scanner r = new Scanner(System.in);
operation = r.nextInt();
switch (operation) {
case 1:
// الكود نفسه من التمارين السابقة
* TODO: تحقق من رقم الطالب المُعطى
وقم بإنشاء Student-Object جديد
فقط في حال كان رقم الطالب صالح
*/
break;
case 2:
// الكود نفسه من التمارين السابقة
break;
case 3:
/* TODO
a) اطلب رقم الطالب
b) أعط معلومات الطالب على الشاشة أو رسالة خطأ في حال عدم تواجده
*/
break;
case 4:
// لاحقا
break;
case 5:
/* TODO
a) اطلب رقم الطالب
b) احذف الطالب من النظام أو أعط رسالة خطأ حال عدم تواجده
*/
break;
case 6:
/* TODO
a) اطلب معلومات المدرس من المستخدم
b) استخدم هذه المعلومات لإنشاء عنصر من صنف المدرس حال عدم تواجده
*/
break;
case 7:
/* TODO
a) أعطِ أسماء وكنى جميع المدرسين المخزنين في النظام
*/
break;
case 8:
/* TODO
a) اطلب اسم وكنية المدرس
b) أعط معلومات المدرس أو رسالة خطأ حال عدم تواجده
*/
break;
case 9:
// spaeter
break;
case 10:
/* TODO
a) اطلب اسم وكنية المدرس
b) احذف المدرس من النظام أو أعط رسالة خطأ حال عدم تواجده
*/
break;
case 11:
/* TODO
a) اطلب معلومات الدرس من المستخدم
b) استخدم هذه المعلومات لإنشاء عنصر من صنف الدرس حال عدم تواجده
*/
break;
case 12:
/* TODO
a) أعط رموز وأسماء جميع الدروس المخزنة على الشاشة
*/
break;
case 13:
/* TODO
a) اطلب رمز الدرس
b) أعط معلومات الدرس أو رسالة خطأ حال عدم تواجده
*/
break;
case 14:
// spaeter
break;
case 15:
/* TODO
a) اطلب رمز الدرس
b) احذف الدرس من النظام أو أعط رسالة خطأ حال عدم تواجده
*/
break;
}
}
}
}
السؤال الرابع
قم بتطوير برنامجك السابق بحيث يشتمل على المهام التالية:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Universitaet uni = new Universitaet();
int operation = 0;
while (operation != 16) {
System.out.println("What Operation do you want to do?");
System.out.println("1- add Student");
System.out.println("2- view Students");
System.out.println("3- search Student");
System.out.println("4- edit Student info");
System.out.println("5- delete Student");
System.out.println("6- add a Lecturer");
System.out.println("7- view the Lecturers ");
System.out.println("8- search Lecturer");
System.out.println("9- edit Lecturer info");
System.out.println("10- delete Lecturer");
System.out.println("11- add Lesson");
System.out.println("12- view the Lessons");
System.out.println("13- search Lesson");
System.out.println("14- edit Lesson info");
System.out.println("15- delete Lesson");
System.out.println("16- register a Student to a Lesson");
System.out.println("17- asseign Levturer to Lesson");
System.out.println("18- view All the Lessons for a Student");
System.out.println("19- view All the Lessons for a Lecturer");
System.out.println("20- view All Students who registered to a Lesson ");
System.out.println("21- view All Lecturers who assigned to a Lesson");
System.out.println("22- End Programm");
Scanner r = new Scanner(System.in);
operation = r.nextInt();
switch (operation) {
// old Operations + the new Operations from 16 to 21
}
}
}
}
الحلول
ملاحظة: يرجى الانتباه إلى أنه في الحلول أدناه المقصود بـ students_number هو عدد الطلاب ككل، بينما student_number هو رقم الطالب الجامعي.
حل السؤال الأول
Main.java
package myproject;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
University uni = new University("tau", 1000);
int operation = 0;
Scanner r = new Scanner(System.in);
while (operation != 6) {
System.out.println("What Operation do you want to do?");
System.out.println("1- add Student");
System.out.println("2- view Students");
System.out.println("3- search Student");
System.out.println("4- edit Student info");
System.out.println("5- delete Student");
operation = r.nextInt();
switch (operation) {
case 1:
System.out.println("Enter the Name of Student:");
String a = r.next();
System.out.println("Enter the Surname of Student:");
String b = r.next();
System.out.println("Enter the Student Number:");
String c = r.next();
Student s1 = new Student(a,b,c);
uni.addstudent(s1);
break;
case 2:
uni.allStudents();
break;
}
}
}}
Student.java
package myproject;
public class Student {
String name = "";
String surname = "";
String student_number = "";
public Student(String n, String p, String k) {
name = n;
surname = p;
student_number = k;
}
}
University.java
package myproject;
public class University {
private String name = "";
private String adress = "";
private Student[] my_students;
private int students_number = 0;
public University(String n, int s) {
name = n;
my_students = new Student[s];
}
public void setAdress(String s) {
adress = s;
}
public String getName() {
return name;
}
void addstudent(Student p) {
my_students[students_number] = p;
students_number = students_number + 1;
}
public void allStudents() {
for (int i=0; i < students_number; i++) {
Student p = my_students[i];
System.out.println(p.name + " " + p.surname + " " + p.student_number);
}
}
}
حل السؤال الثاني
Main.java
package myproject;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
University uni = new University("tau");
uni.setAdress("Beykoz");
Scanner r = new Scanner(System.in);
int operation = 1;
while (operation != 3) {
System.out.println("What Operation do you want to do?");
System.out.println("1- add Student");
System.out.println("2- view Students");
System.out.println("3- End Programm");
operation = r.nextInt();
if (operation == 1){
System.out.println("Enter the Name of Student:");
String a = r.next();
System.out.println("Enter the Surname of Student:");
String b = r.next();
System.out.println("Enter the Student Number:");
String c = r.next();
Student s1 = new Student(a,b,c);
if(uni.my_students.contains(s1) == false){
uni.my_students.add(s1);
}
else{
System.out.println("Student already exists");
}
}
else if(operation == 2){
uni.allStudents();
}
else{
System.out.println("Enter a valid operation number");
}
}
}
}
Student.java
package myproject;
public class Student {
private String name = "unknown";
private String nachname = "unknown";
private String student_number;
private String id = "111111111111";
private int birthdate = 2000;
private double average = 2.0;
private int class = 1;
private String telephon = "5555555555";
private String email = "s@uni.edu";
private String adress = "Global";
public Student(String n, String p, String k) {
name = n;
surname = p;
student_number = k;
}
public Student(String n, String p) {
name = n;
student_number = p;
}
public Student(String n, int p, int k) {
student_number = n;
birthdate = p;
class = k;
}
@Override
public String toString(){
return student_number + ", " + name + " " + surname.toUpperCase();
}
public boolean equals(Object o) {
Student p = (Student) o;
if (student_number.equals(p.student_number)) {
return true;
}
return false;
}
}
University.java
package myproject;
import java.util.Vector;
public class University {
private String name = "uni";
private String adress = "Global";
Vector my_students = new Vector();
private int students_number = 1;
public University(String n) {
name = n;
}
public void setAdress(String s) {
adress = s;
}
public String getName() {
return name;
}
public void allStudents() {
students_number = my_students.size();
System.out.println(my_students);
}
}
حل السؤال الثالث
Main.java
package myproject;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
University uni = new University("tau");
uni.setAdresse("Beykoz");
Scanner r = new Scanner(System.in);
int operation = 0;
while (operation != 16) {
System.out.println("What Operation do you want to do?");
System.out.println("1- add Student");
System.out.println("2- view Students");
System.out.println("3- search Student");
System.out.println("4- edit Student info");
System.out.println("5- delete Student");
System.out.println("6- add a Lecturer");
System.out.println("7- view the Lecturers ");
System.out.println("8- search Lecturer");
System.out.println("9- edit Lecturer info");
System.out.println("10- delete Lecturer");
System.out.println("11- add Lesson");
System.out.println("12- view the Lessons");
System.out.println("13- search Lesson");
System.out.println("14- edit Lesson info");
System.out.println("15- delete Lesson");
System.out.println("16- End Programm");
operation = r.nextInt();
switch (operation){
case 1:
System.out.println("Enter the Name:");
String a = r.next();
System.out.println("Enter the Surname:");
String b = r.next();
System.out.println("Enter the Student Number:");
String c = r.next();
if (Student.test(c)){
System.out.println("the Student Number is not valid");
break;
}
Student s1 = new Student(a,b,c);
if(uni.my_students.contains(s1) == false){
uni.my_students.add(s1);
break;
}
else{
System.out.println("Student already exist");
break;
}
case 2:
uni.allStudents();
break;
case 3:
System.out.println("Enter the Student Number");
String u = r.next();
Student s2 = new Student(u);
uni.search(s2);
break;
case 4:
//later
break;
case 5:
System.out.println("Enter the Student Number");
String u1 = r.next();
Student s3 = new Student(u1);
uni.delete(s3);
break;
case 6:
System.out.println("Enter the Name:");
String a2 = r.next();
System.out.println("Enter the Surname:");
String b2 = r.next();
Lecturer d1 = new Lecturer(a2,b2);
if(uni.my_lecturers.contains(d1) == false){
uni.my_lecturers.add(d1);
break;
}
else{
System.out.println("Lecturer already exist");
break;
}
case 7:
uni.allLecturers();
break;
case 8:
System.out.println("Enter the Name:");
String u2 = r.next();
System.out.println("Enter the Surname:");
String u3 = r.next();
Lecturer d2 = new Lecturer(u2,u3);
uni.searchD(d2);
break;
case 9:
//later
break;
case 10:
System.out.println("Enter the Name:");
String u4 = r.next();
System.out.println("Enter the Surname:");
String u5 = r.next();
Lecturer d3 = new Lecturer(u4,u5);
uni.deleteD(d3);
break;
case 11:
System.out.println("Enter the Code of the Lesson:");
String l = r.next();
System.out.println("Enter the Name of the Lesson:");
String l1 = r.next();
Lesson l3 = new Lesson(l,l1);
if(uni.lesson.contains(l3) == false){
uni.lesson.add(l3);
break;
}
else{
System.out.println("Lesson already exist");
break;
}
case 12:
uni.allLessons();
break;
case 13:
System.out.println("Enter the Code of the Lesson:");
String l4 = r.next();
LVA l5 = new Lesson(l4);
uni.searchL(l5);
break;
case 14:
//later
break;
case 15:
System.out.println("Enter the Code of the Lesson:");
String l6 = r.next();
Lesson l7 = new Lesson(l6);
uni.deleteL(l7);
break;
}
}
}}
Student.java
package myproject;
public class Student extends Person {
private String student_number;
private double average = 2.0;
private int class_num = 1;
public Student(String n, String p, String k) {
name = n;
surname = p;
student_number = k;
}
public Student(String n, String p) {
name = n;
student_number = p;
}
public Student(String n, int p, int k) {
student_number = n;
birthdate = p;
class_num = k;
}
public Student(String m){
student_number = m;
}
@Override
public String toString(){
return student_number + ", " + name + " " + surname.toUpperCase();
}
public boolean equals(Object o) {
Student p = (Student) o;
if (student_number.equals(p.student_number)) {
return true;
}
return false;
}
public static boolean test(String c){
boolean numeric = false;
try{
int k = Integer.valueOf(c);
}
catch(java.lang.NumberFormatException e){
numeric = true;
}
if (c.startsWith("0") || c.length()!=7 || numeric){
return true;
}
else{
return false;
}
}
}
University.java
package myproject;
import java.util.Vector;
public class University {
private String name = "uni";
private String adress = "Global";
Vector<Student> my_students = new Vector();
Vector<Lecturer> my_lecturers = new Vector();
Vector<Lecture> lecture = new Vector();
private int studetns_number = 1;
public University(String n) {
name = n;
}
public void setAdress(String s) {
adress = s;
}
public String getName() {
return name;
}
public void allStudents() {
if (my_students.size() == 0){
System.out.println("There's no students till now");
}
else{
for (int i = 0; i < my_students.size(); i ++){
Student s = (Student) my_students.get(i);
System.out.println(s);
}}
}
public void allLecturers() {
if (my_lecturers.size() == 0){
System.out.println("There's no lecturers till now");
}else{
for (int i = 0; i < my_lecturers.size(); i ++){
Lecturer s = (Lecturer) my_lecturers.get(i);
System.out.println(s);
}}
}
public void allLectures() {
if (lecture.size() == 0){
System.out.println("There's no lectures till now");
}
else{
for (int i = 0; i < lecture.size(); i++){
Lecture s = (Lecture) lecture.get(i);
System.out.println(s);
}}
}
public void search(Student s1){
if (my_students.contains(s1)) {
int e = my_students.indexOf(s1);
System.out.println("Student exists, his Informations are: ");
System.out.println(my_students.elementAt(e));
}else{
System.out.println("Student not found");
}
}
public void searchD(Lecturer s1){
if (my_lecturers.contains(s1)) {
int e = my_lecturers.indexOf(s1);
System.out.println("Lecturer exists, his Informations are:");
System.out.println(my_lecturers.elementAt(e));
}else{
System.out.println("Lecturer not found");
}
}
public void searchL(Lecture s1){
if (lecture.contains(s1)) {
int e = lecture.indexOf(s1);
System.out.println("Lecture exists, its Informations are:");
System.out.println(lecture.elementAt(e));
}else{
System.out.println("Lecture not found");
}
}
public void delete(Student s1){
if (my_students.contains(s1)) {
int e = my_students.indexOf(s1);
System.out.println("Student will be deleted..");
my_students.remove(e);
}else{
System.out.println("Student not found");
}
}
public void deleteD(Lecturer s1){
if (my_lecturers.contains(s1)) {
int e = my_lecturers.indexOf(s1);
System.out.println("Lecturer will be deleted..");
my_lecturers.remove(e);
}else{
System.out.println("Lecturer not found");
}
}
public void deleteL(Lecture s1){
if (lecture.contains(s1)) {
int e = lecture.indexOf(s1);
System.out.println("Lecture will be deleted..");
lecture.remove(e);
}else{
System.out.println("Lecture not found");
}
}
}
Person.java
package myproject;
/**
*
* @author mulham.github.io
*/
public class Person {
protected String name = "unknown";
protected String surname = "unkown";
protected String id = "111111111111";
protected int birthdate = 2000;
protected String telephon = "5555555555";
protected String email = "s@uni.edu";
protected String adress = "Global";
}
Lecture.java
package myproject;
/**
*
* @author mulham.github.io
*/
public class Lecture {
private String Code;
private String Name;
public Lecture(String a, String b){
Code = a;
Name = b;
}
public Lecture(String a){
Code = a;
}
@Override
public String toString(){
return Code.toUpperCase() + " " + Name;
}
public boolean equals(Object o) {
Lecture p = (Lecture) o;
if (Code.equals(p.Code)) {
return true;
}
return false;
}
}
Lecturer.java
package myproject;
/**
*
* @author mulham.github.io
*/
public class Lecturer extends Person {
public Lecturer(String n, String p) {
name = n;
surname = p;
}
public Lecturer(String n) {
name = n;
}
@Override
public String toString(){
return name + " " + surname.toUpperCase();
}
public boolean equals(Object o) {
Lecturer p = (Lecturer) o;
if (name.equals(p.name)) {
return true;
}
return false;
}
}
يرجى مراسلتي ﻷي مشكلة أو سؤال.
المرجع: منهاج أساسيات البرمجة الموجهة، د.بورجو يلدز - الجامعة التركية الألمانية