logo

Crowdly

Browser

Add to Chrome

367.044, VL Vertiefung Softwareentwicklung, Karin Anna Hummel, 2024S

Looking for 367.044, VL Vertiefung Softwareentwicklung, Karin Anna Hummel, 2024S test answers and solutions? Browse our comprehensive collection of verified answers for 367.044, VL Vertiefung Softwareentwicklung, Karin Anna Hummel, 2024S at moodle.jku.at.

Get instant access to accurate answers and detailed explanations for your course questions. Our community-driven platform helps students succeed!

A simple, text-based MVC pattern should be implemented, which increments a counter upon user input via the console. A counter is incremented whenever a user presses "return" (model). The value that is displayed is the counter multiplied by a factor (value = counter * factor). Multiple views should be supported. The code - except the code for the model - is given as follows: 

public class Quiz4 { 

 

    static interface ModelListener {

        public void action(ModelEvent e);

    }

    

    static class ModelEvent {

        private final int counter;

        ModelEvent(int counter){

            this.counter = counter;

        }

        public int getCounter() {

            return counter;

        }

    }

static class View {

View(Model model,int factor) {

ModelListener l = e -> System.out.println("Counter x factor: " + e.getCounter()*factor);

model.addListener(l);

}

}

static class Control {

public void requestInput(Model model) {

while (true) {

System.out.print("Press return: ");

try {

// read all characters until newline

while (System.in.read()!='\n');

} catch (IOException e) { e.printStackTrace(); }

model.setCounter(model.getCounter()+1);

}

}

}

public static void main(String[] args) {

// One model, one controller, two views

Model model = new Model();

View view = new View(model,1);

View view1 = new View(model,2);

Control control = new Control();

control.requestInput(model);

}

}

Which of the following implementations of the model are correct (i.e., bug-free and supporting the required behavior)?

a)

    class Model {

        int counter = 0;

        List<ModelListener> ml = new ArrayList<>();

       

        public int getCounter() { return counter; }

        public void setCounter(int counter) {

            this.counter = counter;

        }

        public void addListener(ModelListener l) {

            ml.add(l);       

        }       

    }

b)

    class Model {

        int counter = 0;

        List<ModelListener> ml = new ArrayList<>();

       

        public int getCounter() { return counter; }

        public void setCounter(int counter) {

            this.counter = counter;

            for (ModelListener l : ml) {

                l.action(new ModelEvent(this.counter));

            }

        }

        public void addListener(ModelListener l) {

            ml.add(l);           

        }       

    }

c)

class Model {

int counter = 0;

ModelListener l = null;

public int getCounter() { return counter; }

public void setCounter(int counter) {

this.counter = counter;

if (l != null) {

l.action(new ModelEvent(this.counter));

}

}

public void addListener(ModelListener l) {

this.l = l;

}

}

d)

class Model {

int counter = 0;

List<ModelListener> ml = new ArrayList<>();

public int getCounter() { return counter; }

public void setCounter(int counter) {

this.counter = counter;

for (ModelListener l : ml) {

l.action(new ModelEvent(this.counter));

}

}

public void addListener(ModelListener l) {

ml = null;

}

}

0%
0%
0%
0%
View this question

Which of the following statements about MVC (model-view-controller) are true?

a) MVC is an architectural pattern (uses the software design patterns observer, composite).

b) MVC allows to have multiple views operating with the same model.

c) The listeners of mouse events or button clicks are implemented in the model.

d) The model listeners are implemented in the model.

0%
0%
0%
0%
View this question

Which of the following statements about Java Swing are true?

a) (Non-empty) user interfaces are compositions of JComponents.

b) Java AWT provides basic classes that may be used by Swing components.

c) Layout managers provide re-/painting methods of an area.

d) When designing a chess game, a GridLayout is a good choice for the game field.

View this question

The following code is intended to show quotes; it provides a window, a button, a text (JLabel), and another button:

        JFrame frame = new JFrame("Quote");

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        frame.setSize(400, 300);         

        frame.setLayout(new BorderLayout());

        

        JButton quoteButton = new JButton("Show Quote");

        frame.add(quoteButton,BorderLayout.NORTH);

        

        JLabel quoteText = new JLabel("Quote coming soon ...");

        quoteText.setHorizontalAlignment(SwingConstants.CENTER);

        frame.add(quoteText,BorderLayout.CENTER);

        

        JButton anotherQuoteButton = new JButton("Show Quote");

        frame.setVisible(true);

Assume you want to show the quote in the text field when pressing a button with text "Show Quote". Which of the following statements will enable this? (Hint: Note the visibility of GUI elements and whether the listener will alter the text field.) 

a)

quoteText.addActionListener(e -> {

           quoteText.setText("<html> There are 10 kind of people,<br> those who understand binary, <br> and those who don’t.</html>");

           frame.repaint(); });

b)

quoteButton.addActionListener(e-> {

            quoteButton.setText("<html> There are 10 kind of people,<br> those who understand binary, <br> and those who don’t.</html>");

            frame.repaint(); });

c)

anotherQuoteButton.addActionListener(e-> {

            quoteText.setText("<html> There are 10 kind of people,<br> those who understand binary, <br> and those who don’t.</html>");

            frame.repaint();

        });

d)

quoteButton.addActionListener(e-> {

            quoteText.setText("<html> There are 10 kind of people,<br> those who understand binary, <br> and those who don’t.</html>");

            frame.repaint(); });

0%
0%
0%
0%
View this question

Assume that you want to test the class ArrayList of java.util with the following JUnit test class:

class TestArrayList {

    ArrayList<Integer> al;

    @BeforeEach

    void setUp() throws Exception {   

        al = new ArrayList<>();

        al.add(10); al.add(20); al.add(30);

    }

    @AfterEach

    void tearDown() throws Exception {

    }

    @Test

    void test1() {

        al.add(1);

        assertEquals(1,al.get(0));

    }

    

    @Test

    void test2() {

         al.add(1);

        assertFalse(al.contains(1));

    }

    

    @Test

    void test3() {

        assertTrue(al.contains(20));

    }

    

    @Test

    void test4() {

        al.clear();

        assertEquals(10,al.get(0));

    }

}

Which of the test cases result in a success?

a) test1()

b) test2()

c) test3()

d) test4()

0%
0%
0%
0%
View this question

Which of the following statements are true about JUnit5 tests?

a) Before executing a test, you need to be sure about the initial state (values and conditions) of the objects used in this test.

b) In a JUnit test method, an exception thrown by the code under test always leads to an error.

c) A JUnit test case can proof that the code under test has no errors.

d) Several JUnit test class files may be created using/testing the same class under test.

View this question

Assume you specified the following test cases based on the requirements of an IoT (Internet of Things) system that is used for a smart home:

class TestMyIoTSystem {

    MyIoTSystem s = null;

    @BeforeEach

    void setUp() throws Exception {

        s = new MyIoTSystem("Home");

    }

    // Test: Adding a home automation item (name, quantity)

    @Test

    void test1() {

        s.add("Vacuum cleaner",2);

        assertTrue(s.has("Vacuum cleaner"));

    }

    

    // Test: Adding a home automation item, querying the quantity of the item in the system

    @Test

    void test2() {

        s.add("Irrigation system",1);

        assertEquals(1,s.getQuantity("Irrigation system"));

    }

    

    // Test: Adding a home automation item, removing an existing item

    @Test

    void test3() {

        s.add("Irrigation system",1); s.add("Vacuum cleaner",1);

        assertEquals("Removed successfully",s.remove("Vacuum cleaner"));

    }

     

    // Test: Adding a home automation item, removing a non-existing item which should throw an exception

    @Test

    void test4() {       

        s.add("Irrigation system",1);

        assertThrows(Exception.class, () -> s.remove("Vacuum cleaner"));

    }

}

The following code has been implemented to fulfill the test cases following test-driven development:

 public class MyIoTSystem {

    private final String name;

    private List<IoTComponent> list;

    

    private class IoTComponent {

        private String name;

        private int quantity;

        

        IoTComponent(String name, int quantity){

            this.name = name;

            this.quantity = quantity;

        }

        

        public String getName () {

            return name;

        }

        

        public int getQuantity() {

            return quantity;

        }

    }

    public MyIoTSystem(String string) {

        name = string;

        list = new ArrayList<>();

    }

    public void add(String name, int quantity) {

        list.add(new IoTComponent(name,quantity));

    }

    public boolean has(String string) {

        for (IoTComponent c : list){

            if (c.getName().compareTo(string) == 0) {

                return true;

            }

        }

        return false;

    }

    public String remove(String string) {

        for (IoTComponent c : list){

            if (c.getName().compareTo(string) == 0) {

                list.remove(c);

                return ("Removed successfully");

            }

        }

        return null;

    }

}

 

Which of the test cases need additional implementations to run successfully?

a) test1()

b) test2()

c) test3()

d) test4()

 

View this question

Which of the following are bad design choices?

a) Design multiple functions that realize similar functions. 

b) Avoid to use too many method arguments.

c) Pack several elementary functionalities together in one method.

d) Avoid extensive coupling between classes.

0%
0%
0%
0%
View this question

Two threads are implemented to run respectively methodA() and methodB() in order to decrement/increment counter1 and counter2:

...

private static Integer counter1=0;

private static Integer counter2=0;

Thread threadA = new Thread (()->methodA());

Thread threadB = new Thread (()->methodB());

               

threadA.start();

threadB.start();

...

Which of the following implementations of the methods may lead to a deadlock?

a)

   public static void methodA() {

        for (int i=1; i<5; i++) {

            try {

                Thread.sleep(500);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            counter1--;

            counter2++;

        }

    }

    public static void methodB() {

        for (int i=1; i<5; i++) {

            try {

                Thread.sleep(500);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            counter1++;

            counter2--;

        }

    }

b)

    private static void methodA() {

        for (int i=1; i<5; i++) {

            try {

                Thread.sleep(500);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            synchronized (counter1){

               counter1--;

            }

            synchronized (counter2){

                   counter2++;

            }

              

        }

    }

    public static void methodB() {

        for (int i=1; i<5; i++) {

            try {

                Thread.sleep(500);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            synchronized (counter2) {

               counter2--;

            }

            synchronized (counter1) {

                counter1++;

            }

        }

    }

c)

    public static void methodA() {

        for (int i=1; i<5; i++) {

            synchronized (counter2) {

               counter2--;

               try {

                    Thread.sleep(500);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

               synchronized (counter1) {

                counter1++;

               }

            }

        }

    }

    public static void methodB() {

        for (int i=1; i<5; i++) {

            synchronized (counter1) {

               counter1--;

               try {

                    Thread.sleep(500);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

               synchronized (counter2) {

                 counter2++;

               } 

            }

        }

    }

d)

    public static void methodA() {

        for (int i=1; i<5; i++) {

            synchronized (counter1) {

               counter1--;

               try {

                    Thread.sleep(500);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }

    }

    

public static void methodB() {

        for (int i=1; i<5; i++) {

            synchronized (counter1) {

               counter1++;

               try {

                    Thread.sleep(500);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }

    }
0%
0%
0%
0%
View this question

What is true about threads?

a) Threads of one process have access to each other's data (memory).

b) If you wish to define a class that extends MyString and can be used to create a thread, you may define the class to implement the interface Runnable.

c) The method wait() is used to wait for another thread to exit.

d) In every Java application, at least one thread is running.

0%
0%
0%
0%
View this question

Want instant access to all verified answers on moodle.jku.at?

Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!

Browser

Add to Chrome