Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Lista 9
- //1ab
- class Time(private[this] var t: Int){
- if(t <0) t=0
- def timeGet:Int = t
- def timeSet(x: Int) =
- {
- if(x>0)
- {
- t = x
- }
- else
- t = 0
- }
- }
- object Time{
- def apply(t:Int) = new Time(t)
- }
- val x = Time.apply(5)
- x.timeSet(-1)
- x.timeGet
- //2
- //a
- class Time(var hour: Int, var minutes: Int){
- if(hour <0 || hour >24){
- println("Zla godzina, zmieniono na 0")
- hour = 0
- }
- if(minutes <0 || minutes > 60){
- println("Zle minuty, zmieniono na 0")
- minutes = 0
- }
- def before(other: Time):Boolean =
- {
- if(hour < other.hour)
- true
- else
- (hour == other.hour && minutes < other.minutes)
- }
- }
- val x = new Time(12,0)
- val y = new Time(13,0)
- x.before(y)
- val z = new Time(11,0)
- x.before(z)
- val q = new Time(-1, 66)
- //b
- class Time(hour: Int, var minutes: Int){
- if(minutes <0 || minutes > 60){
- println("Zle minuty, zmieniono na 0")
- minutes = 0
- }
- if(hour <0 || hour >24){
- println("Zla godzina, zmieniono na 0")
- }
- else
- minutes = minutes + (hour * 60)
- def before(other: Time):Boolean =
- {
- minutes < other.minutes
- }
- }
- val x = new Time(12,0)
- val y = new Time(13,0)
- x.before(y)
- val z = new Time(11,0)
- x.before(z)
- val q = new Time(-1, 66)
- //3.
- //a
- class Pojazd(val producent:String,val model:String,val rok:Int, var rejestracja:String) {
- def this(producent:String, model:String) = {
- this(producent,model,-1,"")
- }
- def this(producent:String, model:String, rok:Int) = {
- this(producent,model,rok,"")
- }
- def this(producent:String, model:String, rejestracja:String) = {
- this(producent,model,-1,rejestracja)
- }
- }
- //to jest konstruktor główny bo val i var są jednocześnie polami klasy
- //btw: Argument konstruktora powinien być jako zwykły argument, gdyż użycie deklarowanie go jako val
- //spowoduje utworzenie dodatkowego pola prywatnego, do którego zostanie skopiowana zawartość
- //tego argumentu oraz getter dla niego, z kolei zadeklarowanie go jako var powoduje to samo +
- //utworzenie settera dla niego (co umożliwia zmianę zawartości tego pola)
- //b
- public class Pojazd {
- private final String producent;
- private final String model;
- private final int rok;
- private String rejestracja;
- public Pojazd(String producent, String model, int rok, String rejestracja) {
- this.producent = producent;
- this.model = model;
- this.rok = rok;
- this.rejestracja = rejestracja;
- }
- public Pojazd(String producent, String model, int rok) {
- this(producent, model, rok, "");
- }
- public Pojazd(String producent, String model, String rejestracja) {
- this(producent, model, -1, rejestracja);
- }
- public Pojazd(String producent, String model) {
- this(producent, model, -1, "");
- }
- public String getRejestracja() {
- return rejestracja;
- }
- public void setRejestracja(String rejestracja) {
- this.rejestracja = rejestracja;
- }
- public String getProducent() {
- return producent;
- }
- public String getModel() {
- return model;
- }
- public int getRok() {
- return rok;
- }
- }
- //4.
- class UzycieWyjatkow {
- def main(args: Array[String]): Unit =
- {
- try {
- metoda1()
- }
- catch {
- case e:Exception => {
- println(e.getMessage())
- e.printStackTrace()
- }
- }
- }
- def metoda1() = metoda2()
- def metoda2() = metoda3()
- def metoda3() = throw new Exception("Wyjatek zgloszony w metoda3")
- }
- val x = new UzycieWyjatkow()
- x.main(new Array[String](10))
- at $line3.$read$$iw$$iw$UzycieWyjatkow.metoda3(<console>:23)
- at $line3.$read$$iw$$iw$UzycieWyjatkow.metoda2(<console>:22)
- at $line3.$read$$iw$$iw$UzycieWyjatkow.metoda1(<console>:21)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement