Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Java ‫ תכנות מתקדם‬,‫פתרון מועד א' תשפ"ב‬

1 ‫שאלה‬
.‫א‬
public class A {
int n1;
String s1;

public A() { //zero arguments constructor


n1=5;
s1="Hello";
}

public A(int n11, String s11) { //2 arguments constructor


n1=n11;
s1=s11;
}

public A(A ref) { //copy constructor


n1=ref.n1;
s1=ref.s1;
}

public void Is_It() {


if (s1.length()>=n1)
System.out.println("EMET");
else
System.out.println("SHEKER");
}

public void print() {


System.out.println("n1= "+n1);
System.out.println("s1= "+s1);
}

.‫ב‬

public class B extends A {


int b1,b2;
private String sb2;

public B() { //zero arguments constructor


super();
b1=120;
b2=120;
sb2="Bye";
}

public B(B ref) { //copy constructor


b1=ref.b1;
b2=ref.b2;
sb2=ref.sb2;
n1=ref.n1;
s1=ref.s1;
}

public int String_len() {


return sb2.length();
}

public void print() {


System.out.println("b1: "+b1);
System.out.println("b2: "+b2);
System.out.println("sb2: "+sb2);
super.print();
}

public void set_sb2(String str) {


sb2=str;
}

public String get_sb2() {


return sb2;
}

2 ‫שאלה‬
.‫א‬
public Student(String name, String id)
{
this.name=name;
this.id=id;
}

.‫ב‬
public String ToString() {
return ("name:"+name+" id: "+id);
}

.‫ג‬
public Exam(Exam ref)
{
subject=ref.subject;
professor=ref.professor;
studList=ref.studList;
}
.‫ד‬
void toString2() {
System.out.println("subject:"+subject);
System.out.println("professor: "+professor);
for (int i=0;i<studList.length;i++) {
System.out.println(studList[i].toString());
}
}

.‫ה‬

public Exam(String sub, String prof, Student[] std)


{
subject=sub;
professor=prof;
for (int i = 0; i < std.length; i++) {
studList[i] = std[i];

3 ‫שאלה‬
public String[] shita(String str,int num) {
String[] arr = new String[num];
for (int i=0;i<arr.length;i++)
arr[i]=str+i;
return arr;
}

4 ‫שאלה‬
public class Exam_grades extends Exam {
double[] grades_arr;
public Exam_grades(String sub,String prof,Student[] stud)
{
super(sub,prof,stud);
grades_arr = new double[studList.length];
for (int i=0;i<studList.length;i++)
{
grades_arr[i]=0;
}
}

void input_grades() {
Scanner scan = new Scanner(System.in);
for (int i = 0; i < studList.length; i++)
{
System.out.println("Enter grade for student:
"+studList[i].toString());
grades_arr[i] = scan.nextDouble();
}
}
}
5 ‫שאלה‬
.‫א‬
Volume: 1.0 The Box is Empty
Zero constructor
Volume: 6.0 The Box contains Money
Volume: 6.0 The Box contains Money
Copy constructor
Volume: 1.0 The Box is Empty(null wrap)
Zero constructor
Volume: 1.0 The Box contains Dress(White wrap)

B1
Box
X=1
Y=1
Z=1
Inside="Empty"

B3
Box
x=1 B2
y=2 Box
z=3 x=1
inside="Money" y=2
z=3
P1 "Inside="Money
present
X=1
Y=1
Z=1
Inside="Empty""Dress"
Wrap_color="White"
B4

.‫ב‬

)‫ (לא הצבעה כפולה‬new – ‫אובייקט שנוצר הוא אובייקט שלצורך יצירתו השתמשנו ב‬

:‫ולכן‬

Box ‫ אובייקטים ממחלקת‬3 ‫נוצרו‬

Present ‫נוצר אובייקט אחד ממחלקת‬

.‫ג‬

b2 ‫ מצביע על אובייקט‬B4 .‫קיימת הצבעה כפולה אחת‬


‫שאלה ‪6‬‬
‫לא נכון‬ ‫א‪.‬‬
‫נכון‬ ‫ב‪.‬‬
‫לא נכון‬ ‫ג‪.‬‬
‫לא נכון‬ ‫ד‪.‬‬
‫לא נכון‬ ‫ה‪.‬‬

You might also like