Question
Giving a suitable example, explain the effect in C++ of qualifying a member function (method) with virtual. [3 marks] (e) Recode the following Java code
Giving a suitable example, explain the effect in C++ of qualifying a member function (method) with virtual. [3 marks] (e) Recode the following Java code in C++. Minor syntactic errors will not be penalised. class Foo { final int[] v; final int s; public Foo(int n) { s = n; v = new int[n]; } // In Java garbage collection de-allocates arrays // appearing in no-longer-used instances of Foo. // In C++ an alternative solution is required.
(a) (i) In programming languages in general, what is the difference between statically and dynamically-allocated variables? Without using the static keyword, give examples of both types of variable in the C language. [2 marks] (ii) What effect do the static and extern keywords have in C for both forms of variable? [3 marks] (b) There are 32 characters in the ASCII set between 'A' and 'a'. All are printable. A potentially-infinite stream of 8-bit bytes can be made printable by encoding into base 32 and represented by a longer stream just using these characters. Give a syntactically-accurate definition in C for encode32(unsigned char a), called for each byte, which invokes putchar(char c) once or twice per call to render the encoded output. [5 marks] (c) Show how the for statement in C can be used to traverse a linked list. Explain the benefits of this coding style, mentioning whether the continue statement works appropriately. [4 marks] (d) typedef char * mystring; mystring s201 = "201"; mystring s202 = s201; s202[2] += 1; A novice programmer writes the above code. What you think they are intending to do and what two problems might they suffer? [2 marks] (e) An interpreter for a string processing language is written in C. Describe four storage and efficiency-related considerations when designing the module for storing strings for the interpreter? [4 marks
(a) A key/value store holds pairs of strings in a non-volatile memory (NVM). The NVM is mapped as a memory region between two hardcoded virtual addresses, NV START and NV END. This memory is all set to zero as manufacturedld data at the addressed location. Bits can never be cleared. Strings representing keys and values will be fewer than 250 characters long. Eventually the memory will fill up, but sufficient is available for the intended application. The API provides the following two operations: int store(char *key, char *value) // returns non-zero on error, and char *lookup(char *key) // returns NULL if not found. Provide a full C implementation of the store routine, explaining how the lookup function and changes to values under a given key would work, if this is not obvious from your code. Code efficiency is not important. Hint: You can directly access the non-volatile store using a construct such as ((char *)NV_START)[offset]. [10 marks] (b) The following text almost constitutes both a C++ and Java program: class Foo { public: int x, y; }; class Test { private: void f1(Foo p) { p.x = 1; } private: void f2(Foo &q) { q.y = 2; } //[Java]: ignore '&' public: void test() { Foo p; //[Java]: replace with: 'Foo p = new Foo();' p.x = p.y = 99; f1(p); Foo q = p; p = q; f2(q); print("HERE"); } }; (i) Explain the storage structure accessible from variables x and y when control reaches print("HERE") following a call to test(), both in the Java and the C++ interpretations. [3 marks] (ii) For the C++ interpretation, show how adjustments only to the definition of class Foo can cause (useful) debugging-style output to be produced at as many places as possible during the execution of method test(). [7 mark
(a) ButtonCanvas could be built using multiple inheritance. What does this mean in this context? [2 marks] (b) Give two reasons why this might be desirable. [2 marks] (c) Give two complexities that arise in this case. [2 marks] (d) Java interfaces originally contained only abstract methods and static final fields. How did this restriction avoid the complexities of extending multiple classes? [3 marks] (e) Draw a UML diagram for building ButtonCanvas using abstract interfaces rather than multiple inheritance. Explain how your design attempts to preserve the desirable properties arising from multiple inheritance. You do not need to recall the exact UML specification, instead you may provide a key explaining your notation. [9 marks] (f ) Recent versions of Java added default methods to interfaces. What is the impact of this with respect to multiple inheritanc
(a) Describe the differences between primitive types and objects in Java. Consider: (i) the values they contain [1 mark] (ii) where they are stored in memory [1 mark] (iii) how they interact with Java references [1 mark] (b) What are auto-boxing and auto-unboxing? Give an example of how they might cause an exception to be thrown. [4 marks] (c) Consider the following code in which any arbitrary Java type (primitive or object) could be substituted for T. void f(T t) { /* ... */ } T t1 = /* ... */ f(t1); For which substitutions of T can we guarantee that the value in t1 is unchanged after the invocation of f(t1)? Justify your answer. [3 marks] (d) Explain how Java's implementation of generics precludes substituting T with a primitive type. [2 marks] (e) You are asked to redesign the standard library to incorporate an immutable list. Explain the relative merits of: (i) MutableList being a subtype of ImmutableList [2 marks] (ii) ImmutableList being a subtype of MutableList [2 marks] (iii) ImmutableList and MutableList having no common supertype [2 marks] (iv) ImmutableList and MutableList both subtyping CommonList [2 marks]
(a) You are given the following implementation for an element of a list: class Element { int item; Element next; Element(int item, Element next) { super(); this.item = item; this.next = next; } @Override public String toString() { return item + " " + (next == null ? "" : next); } } (i) What does the statement super() mean? [1 mark] (ii) What is the meaning of this in the line this.item = item? [1 mark] (iii) What is the purpose of the annotation @Override? [2 marks] (iv) Rewrite the class to be immutable. You may assume that there are no sub-classes of Element. [2 marks] (b) Use the immutable Element class to provide an implementation of an immutable class FuncList which behaves like an int list in ML. Your class should include a constructor for an empty list and methods head, tail and cons based on the following functions in ML. Ensure that your class behaves appropriately when the list is empty. [6 marks] fun head x::_ = x; fun cons (x,xs) = x::xs; fun tail _::xs = xs; (c) Another developer changes your implementation to a generic class FuncList that can hold values of any type T. (i) This means that FuncList is no longer immutable. Explain why and what could be done to remedy this. [2 marks] (ii) Java prohibits covariance of generic types. Is this restriction necessary in this case? Explain why with an example
(a) What is an object? [2 marks] (b) Give four examples of how object-oriented programming helps with the development of large software projects and explain why each one is helpful. [8 marks] (c) Explain the meaning of the Open-Closed principle. [2 marks] (d) Draw a UML diagram for a design satisfying the Open-Closed principle and explain why it satisfies it. [8 marks]
(a) Explain why it is bad practice for member variables such as x to be public. [2 marks] (b) For each of the lines 6-12, explain its purpose. Illustrate your answer with examples. [7 marks] (c) What could result from a call to c.equals(null), assuming c is a reference to a Child object? [3 marks] (d) What would be the consequences of replacing the equals signature with public boolean equals(Child obj)? [4 marks] (e) Explain why Child should also override the hashCode() method. [4 marks
I need your help with my questions below. I am a beginner at Java programming, so I don't know how I can do the following steps and add them to my code: http://sendanywhe.re/P7BV706M. I am stuck on them.
So I need your help with the following questions below.
If you can give detail feedback on each of my questions and help me solve them.
It will be really appreciable.
Required:
Newest IntelliJ IDEA Community Edition with default settings
Question Number 1: How do I design this part below in GitHub and IntelliJ?
Update the @authors fields in Main.java to have full names;
Build project and check that math game works correctly;
Select whole project, then use VCS / Commit... to commit changes into the local GIT repository. This will bring up a dialog box that allows to select which changes want to add into GIT index and then commit into GIT repository. Make sure Main.java file is ticked, so it will be added;
Enter a useful Commit message
Click the Commit button (just a simple Commit at this stage, not Commit and Push...). If IntelliJ reports errors in code, should cancel the commit and fix them. If it reports warnings, can review them and fix some if like, but then go ahead with the Commit;
Now go to GitHub repository in browser and refresh the page to see if that Commit has been copied up to GitHub? should find that it has NOT been pushed up to GitHub yet - commit has been added to local GIT repository (in the .git folder inside project), but has not been pushed yet;
Use IntelliJ menu: VCS / Git / Push... to push all the recent commits up to the GitHub servers;
Refresh the browser again and should now see commit changes appear on GitHub.
Question Number 2: How do I implement this part in Intellij?
The Java code inside the main() method is getting too long. It is time to start breaking up that code into objects, and use object-oriented programming.
So in this section, Will be adding Question objects to the program, and then changing the main method (in Main.java) so that it cooperates with those Question objects and gets them to do most of the work of the game.
This will make our main method smaller and easier to understand. It also opens up more possibilities for reuse of our code and objects:
Could reuse these Question objects in other quiz programs in the future;
or Could reuse them together with a different kind of Main program such as a Graphical User Interface (GUI).
This process of restructuring our program to make it cleaner and more reusable is called refactoring.
Question Number 3: How do I implement this part in Intellij?
Want to design a new type of object: each Question object will represent one math question. For example, one question like "3 + 11 = 14". This question object will have to know the input values (3 and 11) and the operator (plus) of the question, as well as the expected answer (14). Should be able to ask a Question object to do things like:
prints itself as a quiz question (without showing the answer!);
check to see if a student answer is correct or not;
[maybe?] print a message saying what the correct answer was?
There are lots of possible ways could design the Question class, some good, some not so good. Below is a UML class diagram of one possible design, which should be a reasonably good design for our math game.
Note: there are two different constructors shown in this class diagram:
The lower-level constructor takes the input values and operator as arguments. This makes the constructor easy to write, but requires more work from the client code (the code that calls the constructor) to generate all those values.
The higher-level constructor takes just a random number generator and uses that to generate all the values it needs. This makes the constructor a bit harder to write, but makes the client code easier.
Implement both, and compare them.
Question
-value1 : int
-value2 : int
-operator : String
-answer : int
+showQuestion() // to System.out
+checkAnswer(int response) : bool
+Question(v1:int, v2:int, op:String)
+Question(rand : Random)
Now should implement this Question class in the program, and then use it to generate all the question objects that need in the game. Here are the steps to design this Question class:
In IntelliJ, right-click on the package where want to add the Question class and do: New / Java Class. Type in Question as the name of the class, and then create it.
Inside new class, add the four data fields shown above. For example: private int value1;
Add the showQuestion() method. This has no return type, so it's type will be void. The '+' in the UML class diagram means it is public, so will need: public void showQuestion() { }. Then fill in the code inside that method, so that it prints the question out to System.out, with a question mark on the end.
Add the constructor (whichever one 've chosen). This must also be public, will have no return type (because it always returns a new Question object), and its name must be Question. The input parameters will be the parameters shown in the UML class diagram, but will have to translate that UML notation into Java variable declarations (type, then variable name). The job of the constructor is to initialise all the data fields. As a temporary first version of code inside the constructor, suggest just set them to fixed values, like this: value1 = 3; value2 = 5; etc.
Now comes the exciting bit: using new objects!
Go into Main class, in the main(...) method, and see where are generating the question inputs. Just after that point, add a line like the following one to design a new Question object (the '???' parameters will be different, depending upon which constructor 've chosen to use):
Question q1 = new Question( ??? );
q1.showQuestion();
Run game to see what it does now. It probably prints the question twice? Once because the q1 object is printing the question, and once because old code in main() is also printing the question. So, should now be able to delete the lines of old code that print the question. Run the program again to make sure only print the question once.
Time to fix up that temporary constructor inside the Question class. Edit the code inside the constructor so that it sets up the data fields with the correct values - either the values given as parameters, or by using the rand object to generate the values it needs.
Now try to call the checkAnswer(response) method of r q1 object. Design this just after have read the response from the child playing the game. Ah, will have to implement the checkAnswer method inside Question class before can call it! I suggest that make that method responsible for checking the response against the correct answer, and also printing either a message to congratulate them on getting the correct answer, or to commiserate with them for getting the wrong answer.
Run program again and see what it does. should be able to remove even more code from r main method now, since all the answer checking is done inside the Question.checkAnswer method.
Can extend main method a bit more so that it keeps track of the score? Each time a Question is answered correctly, the score should be incremented by one.
Hint: to know when to increment this score, could save the result of the checkAnswer(...) call into a variable called good, and then add a Java if statement to increment r score only when good is true.
Question Number 4: How do I implement this part in Intellij?
So far, the game only does additions, and maybe subtractions, which is a bit boring. Now extend it so that we get a random mix of addition, subtraction multiplication, and division questions.
There is less guidance in this section, so more room for to problem solve when hit issues. Here are some general guidelines:
should use new Question objects for all these kinds of questions;
The code for choosing (randomly) between different kinds of questions could go inside r Question constructor if are using the constructor with the Random generator parameter, or inside main method if are using the other Question constructor with just integer and String values.
An easy way of choosing between the four different kinds of questions is to use Random number generator to generate an integer from 0..3 as follows, and then decide that 0 means addition, 1 means subtraction, 2 means multiplication, and 3 means division.
int questionType = rand.nextInt(4);
Good division questions are a bit more tricky to generate. Questions like "3 / 7" are too hard for primary-school children! want simple questions that have whole number (integer) answers. One nice way of generating good simple x / y = z questions is to generate small integers for y and z, and then calculate x as the product of y and z. For example. if generate 3 and 5, then ask the question "What is 15 / 3?" and use the 5 as the expected answer.
Great! Now we have a useful maths quiz program that can test all four basic arithmetic operations. (If wanted a version for ng programmers, could perhaps add questions about the modulo operator, %).
But it is not very exciting so far. might like to try implementing some timer features to make the game a bit more competitive?
Speed Test: Time how long the student takes to answer all 10 questions and print out the total time at the end of the game, with a congratulations message if they've done it in less than 10 seconds. Hint: call System.currentTimeMillis() at the beginning and end of the game to get the current system time in milliseconds - subtracting those then dividing by 1000.0 will give the elapsed time in seconds
You are required to implement a solution by
creating a JavaScript program. A GUI (Graphical User Interface) event-driven screen is required. Choose a
background color and a text color of your preference for the screen.
The Input will be captured via text boxes and/or radio buttons. The output will be displayed using text boxes.
In addition, a summary report will be produced using the dynamic web page concept. The processing logic to
process the input will be executed by a Process button. A summary report will be executed when clicking on a
Report button.
A shipping company needs a program to verify that their shipments comply with the maximum capacity (2,400
cubic feet) of their containers. Customers who use their shipping services use boxes of different sizes labeled
type: "A", "B","C". The sizes of these boxes are 25, 35, 50 cubic feet respectively.
The program should receive as input the number of boxes shipped and their type. Customers may use the
three types on a shipment, two of them or just one of them. The program will calculate, using a Process
button, the volume taken by those boxes and display that information on the screen. This procedure may be
repeated two or three times if the customer uses two or three types of boxes respectively.
Finally, the program would produce a summary report by using a Report button. The summary report will list:
the type of box and, the volume taken by the boxes for such type. Finally, the report will list the total volume
taken by the entire shipment as well as a message indicating if it fits on a container or not.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started