算法分析作业代写 Algorithms and analysis代写 JAVA代写
874Assignment 2 算法分析作业代写 IMPORTANT NOTES • If you are asked to develop/design an algorithm, you need to describe it in plain English first, say a paragraph, IMPORTANT NOTES •...
View detailsSearch the whole station
Java 编程考试代写 Instructions Before the start of the examination 1) Place your BUPT and QM student cards on the corner of your desk so that your picture is
Before the start of the examination
1) Place your BUPT and QM student cards on the corner of your desk so that your picture is visible.
2) Put all bags, coats and other belongings at the back/front of the room. All small items in your pockets, including wallets, mobile phones and other electronic devices must be placed in your bag in advance. Possession of mobile phones, electronic devices and unauthorised materials is an offence.
3) Please ensure your mobile phone is switched off and that no alarm will sound during the exam. A mobile phone causing a disruption is also an assessment offence.
4) Do not turn over your question paper or begin writing until told to do.
1) You must not communicate with or copy from another student.
2) If you require any assistance or wish to leave the examination room for any reason, please raise your hand to attract the attention of the invigilator.
3) If you finish the examination early you may leave, but not in the first 30 minutes or the last 10 minutes.
4) For 2 hour examinations you may not leave temporarily.
5) For examinations longer than 2 hours you may leave temporarily but not in the first 2 hours or the last 30 minutes.
a)An abstract class called CarRental is defined in Figure 1. It stores information about the number of days that somebody would like to rent the car for, as well as the price per day. Answer the questions below.
public abstract class CarRental { protected int days; // number of days booked protected double price; // price per day public int getDays() { return days; } public double getPrice() { return price; } public void setDays(int n) { days = n; } public void setPrice(double p) { price = p; } /* Calculate and return the cost */ public abstract double calCost(); }
Figure 1
i) Using the abstract class CarRental, write a concrete sub-class called SimpleCarRental. Add a new attribute tax , as well as the accessor (getter) and mutator (setter) for the tax variable. This represents the tax rate applied when renting a car; for example, a 20% tax rate should be stored as 0.2. The calCost() method in SimpleCarRental class simply calculates the cost using the formula days*price*(1+tax). (6 marks)
ii) Write a test program with a main() method for class SimpleCarRental, that calls the calCost() method and displays the cost for variable values days=3, price=79.5 and tax=0.2. (4 marks)
b) Write a Java program called StringReverser that takes a String from the command line argument and prints out the String in reversed order without any white spaces. Note that the method charAt(int i) can be used to retrieve the character at a particular location in a String object.
For example, executing the following command on the command line:
java StringReverser “EBU4201 Java Programming”
would result in the output gnimmargorPavaJ1024UBE. [7 marks]
c)Answer the following questions related to method overriding:
i) What is method overriding? (2 marks)
ii) When would you use the keyword super in relation to method overriding? (2 marks)
iii) Explain why you might wish to override the following methods defined in the class java.lang.Object:
· public boolean equals(Object obj)
· public String toString()
(4 marks)
a) Answer the following questions:
i) Java has TWO types of memory space. Which memory space is used to store objects? (1 mark)
ii) When is an object marked for garbage collection? (1 mark)
iii) What is a null reference? (1 mark)
iv) Assume a Dog class has a sleep() method. What is wrong with the code fragment in line 2 of Figure 2? How can the problem be fixed? (2 marks)
v) By the end of the code in Figure 2, the Dog called “Bob” is not eligible for garbage collection. Explain why that is the case. How could this object be made eligible for garbage collection? (2 marks)
1. Dog myDog;
2. myDog.sleep();
3. myDog = new Dog("Fido");
4. myDog = new Dog("Bob");
Figure 2
b)This question is about interfaces:
i) Describe in detail what interfaces are. (5 marks)
ii) State TWO similarities and TWO differences between interfaces and abstract classes. (4 marks)
iii) Explain why multiple inheritance is not supported for abstract classes, and how interfaces can be used to provide a kind of multiple inheritance. (4 marks)
c) Consider the Java code fragment in Figure 3 (lines numbered 1–4) and determine the value of the variable s1. Write the loop to print out the value of the s1 variable 10 times. [3 marks]
1 String s1 = new String("I love");
2 String s2 = "Java";
3 String s3 = "";
4 s1 = s1 + " " + s2 + "!" + s3;
Figure 3
d) Consider the code for the TWO Java classes in Figure 4 and determine the output of the program. [2 marks]
public class Person {
public String id = "c ";
public Person() {
System.out.print("Person ");
}
}
-----------------------------------------------------------------
public class Student extends Person {
public Student() { System.out.print("Student "); }
public static void main(String[] args) { new Student().go(); }
void go() {
id = "x ";
System.out.print(this.id + super.id);
}
}
Figure 4
a)Consider the Java class shown in Figure 5. [10 marks]
public class Account {
private static int noAccounts;
private int balance;
void withdraw(int amount) {
int charge;
if (balance < amount) charge = 10;
else charge = 0;
balance = balance – amount – charge;
}
}
Figure 5
Identify all the variables in Figure 5, and provide the following information for each of them:
· the name of the variable;
· the variable type, i.e. whether it is a class variable, instance variable, etc;
· a brief description of the type, e.g. if it is a class variable, then what does that mean, etc;
· the scope and default value.
b) Write a Java method named printTable() that accepts one int parameter representing the number of rows and one char parameter representing the separator. This method should print a Multiplication-Table according to the number of rows (which also defines the number of entries in each row), and the separator.
For example: Calling printTable(3, ‘ ’) should produce the output in Figure 6 (a), whereas calling printTable(4, ‘,’) should produce the output in Figure 6 (b).
Important: You must use nested for loops. [6 marks]
c) Answer the questions below:
i) In Java, how does an object relate to a class? (1 mark)
ii) What is the difference between the private, public and protected access modifiers? (3 marks)
iii) Briefly describe the concepts of pass-by-reference and pass-by-value. (2 marks)
d) Consider the code fragment in Figure 7 and determine what will be printed when grade = ‘c’. Justify your answer. [3 marks]
char grade;
switch (grade) {
case 'a':
System.out.println("Excellent");
case 'b':
System.out.println("Good!");
case 'c':
System.out.println("Average!");
case 'd':
System.out.println("Bad!");
default:
System.out.println("No such grade!");
}
Figure 7
a)Consider the Java class in Figure 8 and answer the questions below.
import java.awt.*;
import javax.swing.*;
public class SimpleApp extends JFrame {
private JLabel label;
private JTextField txtField;
private JButton button;
private int advance = 0;
public SimpleApp() {
setLayout(new FlowLayout());
label = new JLabel("Counter");
add(label);
txtField = new JTextField(advance + "", 10);
txtField.setEditable(false); // The text field is read-only.
add(txtField);
button = new JButton("Advance");
add(button);
setSize(250, 100);
setTitle("SimpleApp");
setVisible(true);
}
public static void main(String[] args) {
new SimpleApp();
}
}
Figure 8
i) The code in Figure 8 compiles. Draw a sketch of what is displayed when the code is run. (5 marks)
ii) When you run the code in Figure 8, nothing will happen if you click the Advance button because there is no associated event handling. (9 marks)
· Describe the first TWO steps required to implement an event handler for the button.
· Write the code for the button’s event handling method such that, every time the button Advance is clicked, it increments the value in the text field by 2 units.
b) You are asked to write a Java program that will display the first n lines of a text file called input.txt (where n is an integer value greater than zero). The program is called NLinesReader and both the text file name and the number of lines to read should be given as command line arguments to the program. It is not necessary to write code to check whether the program is always given the correct number and type of arguments.
Note: In order to access the text file contents, the program must use a FileReader and BufferedReader together, to create the required text file input stream. It must also handle exceptions. [9 marks]
c) Describe the concept of exception in Java. [2 marks]
更多代写:CS新加坡网课托管 托福在家考试作弊 英国统计学代考价格 新西兰留学生经济论文代写 哲学网课托管论文代写 作业代写枪手
合作平台:essay代写 论文代写 写手招聘 英国留学生代写
Assignment 2 算法分析作业代写 IMPORTANT NOTES • If you are asked to develop/design an algorithm, you need to describe it in plain English first, say a paragraph, IMPORTANT NOTES •...
View detailsEBU4201 Introductory Java Programming Java编程考试代考 Instructions Before the start of the examination 1) Place your BUPT and QM student cards on the corner of your desk so Time allowed 2 h...
View detailsCOMPS311 Advanced Java Programming and Mobile Application Development 高级Java编程代写 Instructions: 1. Answer this examination paper in English.2. Read the rubric(s) in the examination pap...
View detailsCOMP S311 Online Exam Time Allowed: 3 hours java编程考试代做 Instructions - Answer ALL questions in Part I. [60%] - Answer no more than FOUR (4) questions in Part II. [40%] - The total mark...
View details