) שרית לולב, דורית ליקרמן,הקבצים הנלווים למעבדות (אהובה שפרלינג 1) Array import java.util.*; //Dorit Lickerman 15.4.15 public class Test { public static void main(String[] args) { Scanner S1 = new Scanner(System.in); System.out.println("enter Array Length"); int n=S1.nextInt(); Array A1 = new Array(n); //int [] B1= new int [n]; A1.printArray(); //System.out.println("Array is sort by bubble Sort"); //A1.bubbleSort(); //System.out.println("Array is sort by placement Sort"); //A1.placementSort(); //B1= A1.sortByIndex(); //A1.printByIndex(B1); //A1.printArray(); //A1.printArray(); //int num = A1.series(); //if (num != 0) // System.out.println ("the Array is sub-serise with length: "+ num); //else // System.out.println("the Array is not sub-serise"); //A1.function1(18); //A1.function2(); //A1.printArray(); } } import java.util.*; public class Array { private int [] A; public Array (int len) { this.A = new int [len]; Scanner S1 = new Scanner(System.in); System.out.println("enter numbers to array"); for(int i=0;i<this.A.length;i++) { this.A[i]=S1.nextInt(); } } public int len() { return this.A.length; } public void bubbleSort() { System.out.println("Bubble Sort"); int temp; for(int i=this.A.length-1;i>-1;i--) { for (int j=0;j<i;j++) { if (this.A[j]>this.A[j+1]) { temp=this.A[j]; this.A[j]=this.A[j+1]; this.A[j+1]=temp; } } } } public int indexOfCurrentMax(int index) { int max=A[0]; int pos=0; for(int i=0;i<index+1;i++) { if (this.A[i]>max) { max=this.A[i]; pos=i; } } return pos; } public void placementSort() { System.out.println("Placement Sort"); int temp; int pos; for (int i=this.A.length-1;i>-1;i--) { pos=this.indexOfCurrentMax(i); temp=A[pos]; A[pos]=A[i]; A[i]=temp; } } public int [] sortByIndex() { System.out.println("Sort by Index"); int [] B= new int [this.A.length]; for (int i=0;i<this.A.length;i++) { B[i]=i; } int temp; for(int i=this.A.length-1;i>-1;i--) { for (int j=0;j<i;j++) { if (this.A[B[j]]>this.A[B[j+1]]) { temp=B[j]; B[j]=B[j+1]; B[j+1]=temp; } } } return B; } public void printArray() { for(int i=0;i<this.A.length;i++) { System.out.print(this.A[i]+" "); } System.out.println(); } public void printByIndex(int [] B) { for(int i=0;i<B.length;i++) System.out.print(this.A[B[i]]+" "); System.out.println(); } //return the numbers of times that sub-series appear in the series //for example: [3 4 1 8 3 4 1 8 3 4 1 8 ] -> 3 public int series() { boolean flag=true; int len=1; while ((A[0]!=A[len])&& (len<=this.A.length/2)) { len++; } if ((this.A.length%len!=0) || (len== this.A.length)) { flag=false; } else { int start=0; int i=len; while ((i<this.A.length)&&(A[start]==A[i])&&(flag)) { start++; i++; } if (i!=this.A.length) flag=false; } if (flag) { for (int i=0;i<len;i++) System.out.print(this.A[i]+" "); System.out.println(); len= (this.A.length/len); } else len=0; return len; } //2 בוחן1 תרגיל public void function1(int M) { int [] B = new int [this.A.length]; int f = -1; for (int i=0; i<this.A.length-1; i++) { for(int j=i+1; j<this.A.length ; j++) { if(M == this.A[i]*this.A[j]) { f++; B[f]=this.A[i]; B[f+1]=this.A[j]; f++; } } } if (f==0) System.out.println(";)"התנאי לא מתקיים else for(int i=0; i<f+1; i++) System.out.print(B[i]+" "); } //2 תרגיל2 בוחן public void function2() { int count=0;; for (int i=0; i<this.A.length-1; i++) { for(int j=i+1; j<this.A.length ; j++) { if(this.A[i]>this.A[j]) { count++; } } A[i]=count; count=0; } A[this.A.length-1]=0; } } ----------------------------------- end -------------------------------------2) Calendar public class Test { // Dorit Lickerman March 2015 public static void main(String[] args) { Calendar C1= new Calendar(5,2015); C1.printCalander(); } } public class Month { private String name; private int firstDay; private int numOfDays; public Month () { this.name="January"; this.firstDay=1; this.numOfDays=31; } public Month (int num, int day, int year) { this.name=this.monthName(num); this.firstDay=day; this.numOfDays=this.monthDays(num, year); } public String monthName (int num) { switch (num) { case 1:return"January"; case 2:return"February"; case 3:return"March"; case 4:return"April"; case 5:return"May"; case 6:return"June"; case 7:return"July"; case 8:return"August"; case 9:return"September"; case 10:return"October"; case 11:return"November"; default:return"December"; } } public int monthDays (int num,int year) { switch (num) { case 1:return 31; case 2: { if (year%4==0 && year%100!=0 || year%400==0) return 29; else return 28; } case 3:return 31; case 4:return 30; case 5:return 31; case 6:return 30; case 7:return 31; case 8:return 31; case 9:return 30; case 10:return 31; case 11:return 30; default:return 31; } } public int getFirstDay() { return this.firstDay; } public int getNumOfDays() { return this.numOfDays; } public void printMonth() { System.out.println(this.name); System.out.println("S"+"\t"+"M"+"\t"+"T"+"\t"+"W"+"\t"+"T"+ "\t"+"F"+"\t"+"S"); for (int i=1;i<this.firstDay;i++) { System.out.print("\t"); } for (int i=1;i<this.numOfDays+1;i++) { System.out.print(i); if ((i+this.firstDay-1)%7==0) { System.out.print("\n"); } else System.out.print("\t"); } System.out.print("\n"); } } public class Calendar { private int year ; private Month [] M1; public Calendar (int firstDay, int year) { this.year=year; this.M1=new Month [12]; for (int i=0;i<this.M1.length;i++) { this.M1[i]= new Month(i+1,firstDay,year); //this.M1[i].setMonth(i+1, firstDay , year); firstDay= (firstDay-1+this.M1[i].getNumOfDays())%7+1 ; } } public void printCalander() { for (int i=0; i<12;i++) this.M1[i].printMonth(); } } ----------------------------------- end -------------------------------------3) DieDemo public class Die { private int num; public Die () { this.num = (int)(Math.random()*6+1); } public void roll () { this.num = (int)(Math.random()*6+1); } public int getNum () { return this.num ; } public String toString() { return "The number is :" + this.num; } } public class DieDemo { public static void main(String[] args) { } } ----------------------------------- end -------------------------------------4) Factory public class Test { /** * @param args */ public static void main(String[] args) { } } public class Worker { private String workerName; //שם העובד private double numOfHours; //מספר שעות רגילות private double numExtraHours;//מספר שעות נוספות private double payPerHour; //שכר לשעה public Worker( String name, double payPerH) // מקבלת מהמשתמש את שם העובד ושכר לשעה,פעולה בונה { this.workerName=name; this.numOfHours=0; this.numExtraHours=0; this.payPerHour=payPerH; } public String getName( )// שם העובד- מחזירה את ערך התכונה,פעולה מאחזרת { return this.workerName; } public void setHours(double hours) // משנה את ערך התכונה מספר שעות עבודה,פעולה מעדכנת { this.numOfHours=hours; } public String toString( )// עובד- מחזירה מחרוזת המתארת את העצם,פעולה מאחזרת { return this.workerName + ":" + this.payPerHour; } } ----------------------------------- end of Factory -------------------------------------5) MyBucket public class Test { /** * Bucket problem 14.9.14 */ public static void main(String[] args) { } } import unit4.bucketLib.Bucket; public class MyBucket { private Bucket bucket; private static int count=0; public MyBucket(int capacity) { MyBucket.count++; this.bucket=new Bucket(capacity, "B"+MyBucket.count+":"+capacity+" Liters"); } private Bucket getMyBucket() { return this.bucket; } public void empty() { this.bucket.empty(); } public boolean isEmpty() { return this.bucket.isEmpty(); } public void fill() { this.bucket.fill(this.bucket.getCapacity()); } public double getCurrentAmount() { return this.bucket.getCurrentAmount(); } public void pourInto(MyBucket bucketInto) { this.bucket.pourInto(bucketInto.getMyBucket()); } public String toString() { return this.bucket.toString(); } } ----------------------------------- end -------------------------------------6) RationalNumbers public class RationalNumbers { public static void main(String[] args) { } } public class Rational { //attributes private int numerator; private int denominator; //constructors public Rational() { this.numerator=0; this.denominator=1; } public Rational(int x,int y) { this.numerator=x; this.denominator=y; } //return numerator public int getNumerator() { return this.numerator; } //return denominator public int getDenominator() { return this.denominator; } //update numerator public void setNumerator(int x) { this.numerator=x; } //update denominator public void setDenominator(int x) { this.denominator=x; } //return false if denominator is zero public boolean isLeagal() { return this.denominator!=0; } //return true if denominator and numerator of both numbers are equals public boolean isEqual(Rational num) { boolean fleg; fleg=this.isLeagal(); if (fleg) return (this.getNumerator() == num.getNumerator() && this.getDenominator() == num.getDenominator()); else return false; } //return the reduction of the rational number public void reduction() { int num; if (this.denominator>this.numerator) num=this.denominator; else num=this.numerator; num=(int)Math.sqrt(num); for (int i=num;i>1;i--) if ((this.numerator%i==0)&&(this.denominator%i==0)) { this.numerator=this.numerator/i; this.denominator=this.denominator/i; } } //return string that represents the rational number public String toString() { String str = this.numerator + "/" + this.denominator; return str; } } ----------------------------------- end -------------------------------------7) RectangleProject public class Rectangle { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double getLength() { return this.length; } public double getWidth() { return this.width; } public boolean isSquare() { return this.width==this.length; } public double getDiagonal() { return Math.sqrt(this.length*this.length+this.width*this.width); } public void setLength(Double length) { this.length = length; } public void setWidth(Double width) { this.width = width; } public String toString() { return "Rectangle the Length is : " + this.length + " the Width is : " + this.width; } } public class RectangleMain { public static void main(String[] args) { } } ----------------------------------- end -------------------------------------8) TurtleDemo import unit4.turtleLib.Turtle; public class TurtleDemo { public static void main(String[] args) { } } ----------------------------------- end --------------------------------------