Lecture 8 - Swing continued Flashcards

1
Q

Why do we need a Model-View-Controller style for programming?

A

Otherwise, code will become unreadable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does the use of Model-View-Controller style programming vary by size?

A

Most simple: MVC in one class

Medium size & complexity: Program may have the a class with the View and Controller & a main class that creates 2 objects (View-Controller object & Model object)

Longest & most complex programs may have the MVC separate. 
View (constructor to create display, helper methods to update display)
Controller (actionPerform & other helper methods for the interaction)
Main class creates 3 objects (Model, View, Controller)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the event listener KeyListener do?

A

It detects keys

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What imports do we need for KeyListener?

A

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does charAt(0) do?

A

Returns the char value at the specified index.

An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a for loop that copies something into an arrray,

A

for(int i=0;i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the class header for using a KeyListener and an ActionListener?

A

public class ex extends JFrame implements ActionListener{

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

“b” handles events, but constructs the variables/array in another class “a”.

In “b” you want to get the variable String keyword that you are passing to the constructor and create a new object of class “a”.

How do you do this?

A

String text = keyword.getText();

a = new a (text);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the “model” class?

A

Holds the data

Private variables, methods to perform calculations, getters & speaks to the controller but doesn’t know the view exists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the “view” class?

A

The interface.

  • Swing stuff (ie., extends, components, panels …)
  • Getters (ie. getTextField())
  • Setters for the controller to use to set the calculations in the Model class with the input from the View class.
    • An actionListener is added to the button so when its clicked the controller is alerted
  • Handle errors using a JOptionPane that shows an error message
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the “controller” class?

A

It handles interactions between the view and the model in an actionPerformed() method, but doesn’t actually do anything itself.

Implements ActionListener

Think of it as the SERVER in the client-database relationship.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you get the input from a text field and return is as an Integer?

A

use a get()

return Integer.parseInt(.getText())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is typically in the “view” class?

A
  • extends JFrame so it needs all the information to set up a JFrame
  • private global variables ie.,JLabel
  • a constructor that has the JFrame methods ie., setSIze etc, adds the variables to the JPanel and then adds them to this JPanel
  • getters to access the variables in other classes (JTextField will need to be converted to an int if we are looking for numbers etc)
  • an ActionListener method that lets the controller know that the button has been clicked etc
  • JOptionPane that shows an error if the input is incorrect
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is typically in the “controller” class?

A
  • A constructor that takes in the Model & the View class
  • Handles events by adding actionPerformed method
  • Includes a try catch block incase there is an unchecked error eg., someone enters 1 number instead of 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you use a JPanel that you made in another class?

A
  • in extends JFrame implements ActionListener
  • create an object with the panel class name and pass the current class “this” into it. This means that everything will be constructed in the JPanel class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is system.exit(0) used for?

A

If we enter 0 it exists the program.

If its another number it lets the OS know that something went wrong.

17
Q

What is a content pane?

A

Is the content displayed inside a JFrame window

18
Q

What will be the value of b after this code?

int a = 3; int b = a; a += 2;

A

b is initialised as the same value as a, but it is a different variable. When we change a, b doesn’t change

The correct answer is: 3

19
Q

What will be the contents of a at the end of this code? Drag the numbers into the correct order.

public class ArrayQuestion {
   public static void main(String[] args) {
      int[] a = {3,5,2,1};
      int temp = a[1];
      a[1] = a[2];
      int temp2 = a[3];
      a[3] = temp;
      a[2] = temp2;
   }
}
A

3,2,1,5

20
Q

What will be the output produced on line 16?

Public class QQ {
   private int aNumber;
   public QQ(int a) {
      aNumber = a;
   }
   public void increment() {
      aNumber += 1;
   }
   public int getNumber() {
      return aNumber;
   }
   public static void main(String[] args) {
      QQ a = new QQ(4);
      QQ b = a;
      a.increment();
      System.out.println(b.getNumber()); // LINE 16
   }
}
A

Both references a and b refer to the same object. When the value of aNumber is increased in the object, it will be seen from both references.

The correct answer is: 5

21
Q

Explain how this works?

public static void main(String[] args) {
for(int i=1;i<3;i*=2) {
for(int j=0;j<i></i>

A

i=0, checks it meets the condition i<3 and if it does it goes into the inner loop where j=0.
j=0 keeps looping while the condition j<i></i>

22
Q

What is the result of the following calculation in Java:

5/2*1.0

A

2.0

23
Q

Suppose you are writing a Java method to add two numbers together. Which of the following method names would best follow Java convention?

Select one:

a. addTogether
b. AddTogether
c. addtogether
d. Add_Together

A

addTogether

24
Q

What will be the value of e?

Public class Method {
   public static void main(String[] args) {
      int d = 1;
      int e = method1(d);
   }
   public static int method1(int a) {
      return method2(a) + 2;
   }
   public static int method2(int a) {
      return a + 3;
   }
}
A

6

25
Q

One of the lines in this code includes a syntax error. Which is it?

  1. public class QQQ {
  2. public static void main(String[] args) {
  3. int a = 3;
  4. {
  5. System.out.println(a);
  6. int b = 2;
  7. }
  8. System.out.println(b);
  9. }
  10. }
A

8

26
Q

There is a syntax error in this code. Which line?

  1. public class AnimalWithLegs {
  2. private int nLegs;
  3. public AnimalWithLegs(int a) {
  4. nLegs = a;
  5. }
  6. }
    7.
  7. public class Dog extends AnimalWithLegs {
  8. public Dog() {
  9. super(4);
  10. }
  11. public String toString() {
  12. return “I have “ + nLegs + “ legs”;
  13. }
    15.}
A

13

27
Q

What is the result of the following calculation?

3/4) + (4/3) + 3*(2+1

A

10

28
Q

What will be the output of line 7?

public class QQ {
   public String toString(). {
      return "Hello";
   }
   public static void main(String[] args) {
      QQ[] objs new QQ[5];
      System.out.println(objs[0]); // LINE 7
   }
}
A

null

29
Q

How do you turn buttons off?

A

b.setEnabled(false);