Created
January 21, 2016 00:46
-
-
Save Viacheslav77/180a77253cf9fb81c2e9 to your computer and use it in GitHub Desktop.
Phones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IPhone extends Phone { | |
public IPhone() { | |
System.out.println("IPhone constructor"); | |
touch = true; | |
hasWifi = true; | |
screenSize = 3; | |
} | |
@Override | |
final public void call(String number) { | |
System.out.println("IPhone class is calling " + number); | |
} | |
@Override | |
public void sendSMS(String number, String message) { | |
System.out.println("IPhone class is sending sms " + message + " to " + number); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Phone; | |
public class IPhone5 extends IPhone { | |
public IPhone5() { | |
System.out.println("IPhone5 constructor"); | |
screenSize = 4; | |
} | |
/*@Override | |
public void call(String number) { | |
System.out.println("IPhone class is calling " + number); | |
}*/ // ошибка т.к. метод final | |
@Override | |
public void sendSMS(String number, String message) { | |
super.sendSMS(number, message); | |
System.out.println("IPhone5 class is sending sms " + message + " to " + number); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainClass { | |
public static void main(String[] args) { | |
System.out.println(" 1.Посчитать колличество звонков , которые произвели телефоны и общее колличество созданных телефонов \n" + | |
" 2.Написать код для связи телефонов между собой. У каждого телефона будет свой номер. \n" + | |
" При вызове call(x) телефон должен найти собеседника по номеру x из всех доступных \n" + | |
" в данный момент телефонов из всех созданных объектов типа Phoneи вызвать его метод answer().\n \n" | |
); | |
final PhoneList pl = new PhoneList (); | |
Nokia3310 nokia = new Nokia3310(); | |
nokia.setTelNumber("111-11-11"); | |
pl.add(nokia); | |
for(int i=0; i<5; i++){ | |
nokia.callCounter("123-45-67"); | |
nokia.sendSMS("567-78-89", "text message"); | |
System.out.println(" --------------------------------------"); | |
} | |
IPhone iphone = new IPhone(); | |
iphone.setTelNumber("222-22-22"); | |
pl.add(iphone); | |
for(int i=0; i<3; i++){ | |
iphone.callCounter("123-45-67"); | |
iphone.sendSMS("567-78-89", "text message"); | |
System.out.println(" ----------------------------------"); | |
} | |
IPhone5 iphone5 = new IPhone5(); | |
iphone5.setTelNumber("333-33-33"); | |
pl.add(iphone5 ); | |
iphone5.callCounter("123-45-67"); | |
iphone5.sendSMS("567-78-89", "text message"); | |
System.out.println("----------------------------------"); | |
SamsungS4 SamsungS4 = new SamsungS4(); | |
SamsungS4.setTelNumber("444-44-44"); | |
pl.add(SamsungS4 ); | |
SamsungS4.callCounter("123-45-67"); | |
SamsungS4.sendSMS("567-78-89", "text message"); | |
System.out.println(" --------------------------------------"); | |
System.out.println("Колличество звонков"); | |
System.out.println("Nokia3310 " + nokia.callCounter + " Iphone " + iphone.callCounter + " Iphone5 " + iphone5.callCounter + " SamsungS4 " + SamsungS4.callCounter); | |
System.out.println(" --------------------------------------"); | |
System.out.println("Колличество телефонов " + pl.getPhoneCounter()); | |
System.out.println("----------------------------------"); | |
System.out.println("----------------------------------"); | |
Phone p1=pl.findPhone("444-44-44"); | |
System.out.print(iphone5.getClass() + " telNumber " + iphone5.getTelNumber()); | |
iphone5.callTo(p1); | |
System.out.println("----------------------------------"); | |
p1=pl.findPhone("111-11-11"); | |
System.out.print(iphone.getClass() + " telNumber " + iphone.getTelNumber()); | |
iphone.callTo(p1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Phone; | |
public class Nokia3310 extends Phone { | |
public Nokia3310() { | |
System.out.println("Nokia3310 constructor"); | |
touch = false; | |
hasWifi = false; | |
screenSize = 2; | |
} | |
@Override | |
public void call(String number) { | |
super.call(number); | |
System.out.println("Nokia3310 class is calling " + number); | |
} | |
@Override | |
public void sendSMS(String number, String message) { | |
System.out.println("Nokia3310 class is sending sms " + message + " to " + number); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Phone { | |
protected boolean touch; | |
protected boolean hasWifi; | |
protected int screenSize; | |
protected int callCounter ; | |
protected String telNumber; | |
public Phone() { | |
System.out.println("Phone constructor"); | |
} | |
public boolean isTouch() { | |
return touch; | |
} | |
public boolean isHasWifi() { | |
return hasWifi; | |
} | |
public int getScreenSize() { | |
return screenSize; | |
} | |
public void call(String number) { | |
System.out.println("Phone class is calling " + number); | |
} | |
public void callTo (Phone ph) { | |
ph.answer(ph); | |
} | |
public void answer(Phone ph) { | |
System.out.println(" is calling to " + ph.getClass() + " telNumber " + ph.getTelNumber()); | |
System.out.println(ph.getClass() + " is answer " ); | |
} | |
public void callCounter (String number) { | |
callCounter++ ; | |
call( number); | |
System.out.println(" " | |
+ " CallCounter " + callCounter); | |
} | |
public int getCallCounter () { | |
return callCounter ; | |
} | |
public String getTelNumber () { | |
return telNumber ; | |
} | |
public void setTelNumber (String telNumber ) { | |
this.telNumber = telNumber; | |
} | |
public abstract void sendSMS(String number, String message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PhoneList { | |
private Phone [] lst = new Phone [10]; | |
private int PhoneCounter = 0; | |
public void add(Phone ph) { | |
lst[PhoneCounter] = ph; | |
System.out.println(lst[PhoneCounter].getClass().getName() | |
+ " screent size: " +lst[PhoneCounter].getScreenSize() | |
+ " TelNumber " + lst[PhoneCounter].getTelNumber()); | |
System.out.println("-------------------------------------------------------------------------------"); | |
PhoneCounter++; | |
} | |
public Phone get(int n) { | |
return lst[n]; | |
} | |
public int getPhoneCounter () { | |
return PhoneCounter; | |
} | |
public Phone findPhone (String number) { | |
for(int i=0; i<10; i++ ){ | |
if(lst [i].getTelNumber().equals(number)){ | |
return lst [i]; | |
} | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Написать класс наследник SamsungS4 с диагональю экрана 5 дюймов, | |
//поддержкой Wifiи методом отправки SMS, который будет дописывать к сообщению слово “Hello”. | |
public class SamsungS4 extends Phone{ | |
public SamsungS4() { | |
System.out.println("SamsungS4 constructor"); | |
touch = true; | |
hasWifi = true; | |
screenSize = 5; | |
} | |
@Override | |
final public void call(String number) { | |
System.out.println("SamsungS4 class is calling " + number); | |
} | |
@Override | |
public void sendSMS(String number, String message) { | |
System.out.println("SamsungS4 class is sending sms " + message + " to " + number + " Hello!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment