Java

| October 27, 2015

1. (TCOs 1–8) Write a class called point

for the 2-D environment.

Select appropriate variable(s).

Write constructors.

Write access methods for the variables.

Write a Print method to display the variables when called.

Write a test program to test it.

2. (TCOs 1–8) Write a class called rectangle,

which inheres the above point class.

Select appropriate variable(s).

Write constructors.

Write access methods for the variables.

Override the inherited Print method to display variable in rectangle and point.

Write a test program to test it.

3. (TCOs 1–8) Start from the following code, and add Action Listener to make it functional. (Need about 10 lines)

Note: 1 lb = .0.453592 kg.

import javax.swing.*;

import

java.awt.GridLayout;

import

java.awt.event.*;

import

java.text.DecimalFormat;

public class weightConverter extends JFrame {

public static void

main(String[] args) {

JFrame frame = new weightConverter();

frame.setTitle(“Weight”);

frame.setSize(200, 100);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

public

weightConverter() {

JLabel lbllb = new JLabel(“lb”,SwingConstants.CENTER);

JLabel lblkg = new JLabel(“kg”,SwingConstants.CENTER);

final

JTextField jtflb = new JTextField();

final

JTextField jtfkg = new JTextField();

JButton jbtLeft = new JButton(“<="); JButton jbtRight = new JButton("=>“);

JPanel panel = new JPanel(new GridLayout(2, 3));

panel.add(lbllb);

panel.add(jbtLeft);

panel.add(lblkg);

panel.add(jtflb);

panel.add(jbtRight);

panel.add(jtfkg);

this.add(panel); // Add panel to the frame

final

DecimalFormat dec = new DecimalFormat(“#.00”);

}

}

4. (TCOs 1–8) Start from the given class, and create a NewPanel class to draw a figure like below: (Need about a dozen lines)

import javax.swing.*;

import

java.awt.Graphics;

import java.awt.Color;

public class drawFlag extends

JFrame {

public

drawFlag() {

add(new

NewPanel());

}

public static void

main(String[] args) {

drawFlag frame = new drawFlag();

frame.setTitle(“Flag”);

frame.setSize(200, 200);

frame.setLocationRelativeTo(null); // Center the frame

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

The post tip is a solid circle coordinate and size is (68, 28, 4, 4).

The flag tip is a rectangle with coordinate and size is (70, 30, 50, 30).

The text coordinate is (85, 118).

The starting and ending coordinate for the post is (70, 30, 70, 120).

Color blue with text black.

9. (int)(‘a’ + Math.random() * (‘z’ – ‘a’ + 1)) returns a random number _____ (Points : 3)

between ‘a’ and ‘y’.

between 0 and (int)’z’.

between ‘a’ and ‘z’.

between (int)’a’ and (int)’z’.

5b. (TCOs 1–8) Show the output of the following code.

import javax.swing.*;

public class Test {

public static void main(String[ ] args) {

JButton jbtOK = new JButton(“OK”);

System.out.print(jbtOK.isVisible() + “, “);

JFrame frame = new JFrame();

System.out.println(frame.isVisible());

}

} (Points : 3)

true, true

false, true

false, false

true, false

9b. (TCOs 1–8) Which of the following statements is correct? (Points : 3)

Integer.parseInt(100);

Integer.parseInt(100, 16);

Integer.parseInt(“12”, 2);

Integer.parseInt(“345”, 8);

13b. (TCOs 1–8) Analyze the following code.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Test extends A {

public static void main(String[ ] args) {

A frame = new Test();

frame.setSize(200, 100);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

JButton jbtOK = new JButton(“OK”);

public Test() {

getContentPane().add(jbtOK);

jbtOK.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

super.actionPerformed(e);

if (e.getSource() == jbtOK)

System.out.println(“OK button is clicked”);

}

}

class A extends JFrame implements ActionListener {

JButton jbtCancel = new JButton(“Cancel”);

public A() {

getContentPane().setLayout(new FlowLayout());

getContentPane().add(jbtCancel);

jbtCancel.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == jbtCancel)

System.out.println(“Cancel button is clicked”);

}

}
When you click the Cancel button the message “Cancel button is clicked” is displayed.

When you click the OK button the message “OK button is clicked” is displayed.

If the super.actionPerformed(e) statement in the actionPerformed method in the Test class is omitted, no message is displayed if you click the Cancel button.

The program displays Cancel button on the left of the OK button.

All of them.

23b. (TCO 1-8) An object is an instance of a _____ (Points : 3)

method.

class.

data.

program.

24b. (TCOs 1–8) What is the printout of the third println statement in the main method?

public class Foo {

int i;

static int s;

public static void main(String[ ] args) {

Foo f1 = new Foo();

System.out.println(“f1.i is ” + f1.i + ” f1.s is ” + f1.s);

Foo f2 = new Foo();

System.out.println(“f2.i is ” + f2.i + ” f2.s is ” + f2.s);

Foo f3 = new Foo();

System.out.println(“f3.i is ” + f3.i + ” f3.s is ” + f3.s);

}

public Foo() {

i++;

s++;

}

} (Points : 3)

f3.i is 1 f3.s is 2

f3.i is 1 f3.s is 1

f3.i is 3 f3.s is 1

f3.i is 3 f3.s is 3

f3.i is 1 f3.s is 3

5c. (TCOs 1–8) The following program displays _____

public class Test {

public static void main(String[ ] args) {

String s = “Java”;

StringBuilder buffer = new StringBuilder(s);

change(s);

System.out.println(s);

}

private static void change(String s) {

s = s + ” and HTML”;

}

} (Points : 3)

and HTML.

Java and HTML.

Java.

nothing is displayed.

8c. (TCOs 1–8) What is the output of the following code?

public class Test {

public static void main(String[ ] args) {

String s1 = “Welcome to Java!”;

String s2 = s1;

if (s1 == s2)

System.out.println(“s1 and s2 reference to the same String object”);

else

System.out.println(“s1 and s2 reference to different String objects”);

}

}

s1 and s2 reference to different String objects

s1 and s2 reference to the same String object

13c. (TCOs 1–8) To retrieve all courses with more than three credit hours, you write

select * from Course

where numOfCredits > 3;

Is this statement correct? (Points : 3)

Yes

No

Get a 5 % discount on an order above $ 150
Use the following coupon code :
2020Discount
Query 1
guessing game

Category: Homework Help

Our Services:
Order a customized paper today!
Open chat
Hello, we are here to help with your assignments
Powered by