Частина тексту файла (без зображень, графіків і формул):
МІНІСТЕРСТВО ОСВІТИ І НАУКИ, МОЛОДІ ТА СПОРТУ УКРАЇНИ
НУ «ЛЬВІВСЬКА ПОЛІТЕХНІКА»
Кафедра ЕОМ
/
Звіт
з лабораторної роботи №3
з дисципліни “Кросплатформні засоби програмування”
Мета: ознайомитися з процесом розробки та пакетів мовою Java.
Завдання:
Написати та налагодити програму на мові Java, що реалізує у вигляді класу предметну область згідно варіанту. Програма має задовольняти наступним вимогам:
програма має розміщуватися в пакеті Група.Прізвище.Lab3;
клас має містити мінімум 3 поля, що є об’єктами класів, які описують складові частини предметної області;
клас має містити кілька конструкторів та мінімум 10 методів;
для тестування і демонстрації роботи розробленого класу розробити клас-драйвер;
методи класу мають вести протокол своєї діяльності, що записується у файл;
розробити механізм коректного завершення роботи з файлом (не надіятися на метод finalize());
програма має володіти коментарями, які дозволять автоматично згенерувати документацію до розробленого пакету.
Автоматично згенерувати документація до розробленого пакету.
Варіант 21: Пістолет
Код програми:
Main.java:
package KI.Pastushok.lab3;import java.io.FileNotFoundException;/** * Main class implements main method for Pistol class possibilities demonstration * @author Pastuhok.Roman * @version 1.0 * */public class Main { /** * @param args * @throws FileNotFoundException * */ public static void main(String[] args) throws FileNotFoundException { Pistol pistol=new Pistol(); pistol.insertGun(); pistol.addPatronInGun(2); pistol.chargePistol(); pistol.regCurrentCountPatron(); pistol.fire(); pistol.fire(); pistol.regCurrentCountPatron(); pistol.fire(); }}
Pistol.java:
package KI.Pastushok.lab3;import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;/** * Class <code>Pistol</code> implements pistol * @author Pastushok.Roman * @version 1.0 */public class Pistol { private Bolt bolt; private Gun gun; private Handle handle; private PrintWriter fout; /** * Constructor * @throws FileNotFoundException */ public Pistol() throws FileNotFoundException { bolt=new Bolt(); gun=new Gun(); handle=new Handle(); fout=new PrintWriter(new File("myFile.txt")); } /** * Constructor * @param len * @param mat * @param max * @throws FileNotFoundException */ public Pistol(int max,int len,String mat) throws FileNotFoundException { bolt=new Bolt(len,mat); gun=new Gun(max); handle=new Handle(); fout=new PrintWriter(new File("myFile.txt")); } /** * Method implements insert gun in pistol */ public void insertGun(){ gun.setStatusAvailable(true); fout.println("Магазин вставлено у пістолет!"); fout.flush(); } /** * Method implements fire of pistol */ public void fire(){ if(gun.getCurrnetCountPatron()==0){ fout.println("Магазин пустий"); fout.flush(); } else { gun.subPatron(); fout.println("Постріл виконано!"); fout.flush(); } } /** * Method implements add patron in gun of pistol * @param countPatron mean number patrons that will add in gun */ public void addPatronInGun(int countPatron){ if (gun.getCurrnetCountPatron()<gun.getMaxCountPatron()) { for (int i = gun.getCurrnetCountPatron(); i < gun.getMaxCountPatron() && i<countPatron; i++) { gun.addPatron(); fout.println("Новий патрон додано у магазин"); fout.flush(); } } else { fout.println("Магазин повністю наповнений!!!"); fout.flush(); } } /** * Method implements change material of handle * @param mat mean new material of handle */ public void changeHandle(String mat){ handle.setMaterial(mat); fout.println("Матеріал ручки замінений на "+mat); fout.flush(); } /** * Method implements charge pistol */ public void chargePistol(){ if(bolt.getStatus()==true){ fout.println("Пістолет уже заряджений"); fout.flush(); } else{ bolt.charge(); fout.println("Зарядка пістолета відбулась успішно"); fout.flush(); } } /** * Method implements discharge pistol */ public void dischargePistol(){ if(bolt.getStatus()==false){ fout.println("Пістолет уже є розряджений!"); fout.flush(); } else { bolt.discharge(); fout.println("Пістолет був успішно розряджений"); fout.flush(); } } /** * Method implements return current number of patron in gun */ public void regCurrentCountPatron(){ fout.println("У магазині залишилось "+gun.getCurrnetCountPatron()+" патронів"); fout.flush(); }}
Gun.java:
package KI.Pastushok.lab3;/** * Class <code>Gun</code> implements gun of pistol */public class Gun { private int maxCountPatron; private int currnetCountPatron; private Boolean statusAvailable; /** * Constructor * @param maxCountPatron set maximum number of patron in gun * @param status set status of gun */ public Gun(int maxCountPatron, Boolean status) { this.maxCountPatron = maxCountPatron; this.statusAvailable = status; } /** * Constructor */ public Gun(){ this.maxCountPatron=8; this.statusAvailable=false; this.currnetCountPatron=0; } /** * Constructor * @param max set maximum number of patron */ public Gun(int max){ this.maxCountPatron=max; this.statusAvailable=false; this.currnetCountPatron=0; } /** * method which set maximum number of patron in gun */ public void setMaxCountPatron(int count){ this.maxCountPatron=count; } /** * method which return maximum number of patron in gun */ public int getMaxCountPatron(){ return this.maxCountPatron; } /** * method which add patrons in gun */ public int addPatron(){ this.currnetCountPatron++; return currnetCountPatron; } /** * method which sub patrons in gun */ public int subPatron(){ this.currnetCountPatron--; return currnetCountPatron; } /** * method which set status of gun */ public void setStatusAvailable(Boolean st){ this.statusAvailable=st; } /** * method which return patrons in gun */ public Boolean getStatusAvailable(){ return this.statusAvailable; } /** * method which return current number of patrons */ public int getCurrnetCountPatron() { return currnetCountPatron; } /** * method which set current number of patrons */ public void setCurrnetCountPatron(int currnetCountPatron) { this.currnetCountPatron = currnetCountPatron; }}
Bolt.java:
package KI.Pastushok.lab3;/** * Class <code>Bolt</code> implements gun of pistol */public class Bolt { private int length; private String material; private Boolean status; /** * Constructor * @param length set lenght of bolt * @param material set material of bolt * @param status set status of bolt */ public Bolt(int length, String material, Boolean status) { this.length = length; this.material = material; this.status = status; } /** * Constructor */ public Bolt(){ this.length=0; this.material="none"; this.status=false; } /** * Constructor * @param len set lenght of bolt * @param mat set material of bolt */ public Bolt(int len,String mat){ this.length=len; this.material=mat; this.status=false; } /** * method which set material of bolt */ public void setMaterial(String mat){ this.material=mat; } /** * method which set lenght of bolt */ public void setLength(int len){ this.length=len; } /** * method which return material of bolt */ public String getMaterial(){ return this.material; } /** * method which return lenght of bolt */ public int getLength(){ return this.length; } /** * method of charge bolt */ public void charge(){ this.status=true; } /** * method of discharge bolt */ public void discharge(){ this.status=false; } /** * method which ret status of bolt */ public Boolean getStatus(){ return this.status; }}
Handle.java:
package KI.Pastushok.lab3;/** * Class <code>Handle</code> implements gun of pistol */public class Handle { private String material; private String forms; /** * Constructor * @param material set material of handle * @param forms set form of handle */ public Handle(String material, String forms) { this.material = material; this.forms = forms; } /** * Constructor */ public Handle(){ this.material="none"; this.forms="none"; } public String getMaterial() { return material; } public void setMaterial(String material) { this.material = material; } public String getForms() { return forms; } public void setForms(String forms) { this.forms = forms; }}
Результат роботи у текстовому файлі:
/
Згенерова документація:
/
Висновок: під час лабораторної роботи я ознайомився з процесом розробки класів та пакетів мовою Java.