You are on page 1of 5

‫מחלקות‬

1 ‫שאלה‬

:Card ‫המחלקה‬

public class Card {

private int value;


private String shape;

public Card(int value, String shape){


this.value = value;
this.shape = shape;
}

public int getValue(){


return this.value;
}

public String getShape(){


return this.shape;
}

public boolean equalValue(int num){


// ‫דרך א‬
if (this.value == num){
return true;
} else{
return false;
}

// ‫דרך ב‬
//return this.value == num;
}

public boolean equalShape(Card other){


// this = c1 = ‫מי הפעיל את הפעולה‬ , other = c2 = ‫מי הפרמטר‬
return this.shape.equals(other.getShape());
}

public String toString(){


String toString = "Value: " + this.value + " Shape: " + this.shape;
return toString;
}
}
:Main ‫המחלקה‬

public class Main {

public static void main(String[] args) {


String shape1 = "Tiltan";
String shape2 = "Lev";
String shape3 = "Yahalom";
String shape4 = "Alim";

Card c1 = new Card(10, "Tiltan");


Card c2 = new Card(7, "Lev");

// ‫סעיף ב‬
if (c1.equalValue(c2.getValue())){
System.out.println("Yes");
}else {
System.out.println("No");
}

// ‫סעיף ג‬
String newShape = null;

// ‫דרך א‬
String[] shapesArray = new String[4];
shapesArray[0] = shape1;
shapesArray[1] = shape2;
shapesArray[2] = shape3;
shapesArray[3] = shape4;

for (int i = 0; i < shapesArray.length && newShape == null; i++) {


if (!c1.getShape().equals(shapesArray[i]) && !
c2.getShape().equals(shapesArray[i])){
newShape = shapesArray[i];
}
}

// ‫דרך ב‬
//Tiltan
if (!c1.getShape().equals(shape1) && !c2.getShape().equals(shape1)){
newShape = shape1;
}
//Lev
else if(!c1.getShape().equals(shape2) && !c2.getShape().equals(shape2)){
newShape = shape2;
}
//Yahalom
else if(!c1.getShape().equals(shape3) && !c2.getShape().equals(shape3)) {
newShape = shape3;
}
//Alim
else{
newShape = shape4;
}

int maxValue = Math.max(c1.getValue(), c2.getValue());


Card c3 = new Card(maxValue, newShape);

// ‫סעיף ד‬
System.out.println(c3.toString());

// ‫סעיף ה‬
if (c1.equalValue(c3.getValue()) || c2.equalValue(c3.getValue())){
System.out.println("C3 has same value as C1 or C2");
}
}
}
2 ‫שאלה‬

.‫א‬
public class Baby {
private String id;
private char gendfer;
private double height;
private double weight;

public Baby(String id, char gendfer, double height, double weight) {


this.id = id;
this.gendfer = gendfer;
this.height = height;
this.weight = weight;
}

public double getHeight() { return height; }

public double getWeight() { return weight; }

public void updateWeight(double w){


this.weight += w;
}

public void addHeight(double h){


this.height += h;
}

public boolean isSame(Baby other){


return this.height == other.getHeight() && this.weight == other.getWeight();
}
}

B1, B2, B3 ‫לדוגמא עבור‬ .‫ב‬

B1: ("1111", 'M', 70, 3.5)

B2: ("2222",'F',70,2.8)

B3: ("3333",'M',70,3.5)

Output:

***

*!!*
‫ממדיים‬-‫מערכים חד‬
1 ‫שאלה‬

int sum = 0;
for(int i = 0; i < arr.length; i++){
if (arr[i] >= 100 && arr[i] <= 450) {
sum += arr[i];
}
}

System.out.println("The sum of numbers between 100-450 is: " + sum);

2 ‫שאלה‬

int count = 0;
for(int i = 0; i < arr.length; i++){
if (arr[i] >= 10 && arr[i] < 100) {
count++;
}
}

System.out.println("The number of double digit numbers is: " + count);

3 ‫שאלה‬

boolean isValid = true;


for(int i = 1; i < arr.length; i+=2){
if (arr[i] != '*'){
isValid = false;
}
}

if (isValid){
System.out.println("yes");
}else {
System.out.println("no");
}
4 ‫שאלה‬

// ‫סעיף א‬
int countRandomNums = 0;
for(int i = 0; i < arr.length; i++){
if ((i % 2 == 0 && arr[i] != 99) ||
(i % 2 == 1 && arr[i] != 49)){
arr[i] = (int) (Math.random() * 7) + 7;
countRandomNums++;
}
}

// ‫סעיף ב‬
// ‫מערך חדש עבור המספרים הרנדומליים בלבד‬
int[] randomArr = new int[countRandomNums];
int randomArrIndex = 0;

// ‫ עבור בלולאה על כל המערך‬arr ‫שחלקו מכיל מספרים רנדומליים‬


for (int i = 0; i < arr.length; i++) {
// ‫ יש כאן מספר רנדומלי‬- ‫ במיקום אי זוגי‬49 ‫ במיקום זוגי או‬99 ‫במידה ולא מופיע‬
if ((i % 2 == 0 && arr[i] != 99) ||
(i % 2 == 1 && arr[i] != 49)){
// ‫עדכן את המערך החדש במספר הרנדומלי‬
randomArr[randomArrIndex] = arr[i];
randomArrIndex++;
}
}

// ‫סעיף ג‬
for (int i = 0; i < randomArr.length; i++) {
System.out.println(randomArr[i]);
}

‫מחרוזות‬
1 ‫שאלה‬

String st = "HAPPY BIRTHDAY TO YOU";


int count = 0;
for (int i = 0; i < st.length(); i++) {
if (st.charAt(i) == 'Y' && st.charAt(i + 1) == ' '){
count++;
}
}

System.out.println("Number of Y in word: " + count);

You might also like