CAD:+1-604-396-3267 ,IN:+91-7503070001, IN:+91-9519066910
  • My Account
  • solution


    Java Assignment Help

    Java Assignment Help

    Java Assignment Help

    1) Explain (in 3 sentences) the difference between an instance method and a class (static) method 

           2) Give an example of a class constant declaration and explain the role of each reserved word in the declaration.   

           3) How many constructors can a class have? Can you have a class with no constructors? If a class has more than one

               constructor,which of them gets called?

           4) What are the type and the value of the expression
    s == null || s.compareTo(z) > 0 ? z.length() : s.length()
    after the following declarations:
    String s = null; String z = "null";
    Explain your answer (in particular, provide the type and the value of each relevant subexpression).

    Hint: Instance method compareTo of class String compares strings lexicographically: the result is a negative integer if the object string lexicographically precedes the argument string; a positive integer if the object string lexicographically follows the argument string; and it is zero if the two strings are equal.

          Instance method length of class String returns the number of characters in the    string.

    5) Implement a method
    public static int[] difference(int[] goalsFor, int[] goalsAgainst)
    that returns a new array with goal differences given two arrays representing match results: for example, if
    goalsFor is { 2, 0, 2 } and goalsAgainst is { 1, 2, 2 }, then the method should return { 1, -2, 0 }.

    If the two arrays have different length, then the method should return null.

    Important: the method should NOT change the given arrays and should return a new array instead.

    6) Implement a method getGoalDifference that receives two arrays representing match results, as in Question 5, and returns the total goal difference for the series of matches (by adding goal differences for individual games). For example, if getGoalDifference is invoked with goalsFor = { 2, 0, 2 } and goalsAgainst = { 1, 2, 2 }, then the method should return 1 + (-2) + 0 = -1.

    Use the difference method implemented in Question 5 and check if it returns null, in which case the method getGoalDifference should return -999

    7) Consider the following program fragment:

    (a) int a = 1;
       for (int p = 20; p >= 10; p = p - 3) {
         System.out.print(a - 1);
         a = a + p;

    The following two fragments were intended to print out the same output as fragment (a):

    (b) int q = 20;
       while (q >= 10) {
         int b = 1;
         System.out.print(b - 1);
         b += q;
         q -= 3;

    (c) int c = 0;
       int r = 20;
       do {
         r += -3;
         c += r;
      } while (r >= 10);

    What is the output printed in each of the fragments (a), (b) and (c)?

    Indicate how (b) and (c) could be modified (with as few changes as possible) so that the resulting code would produce the same output as

    8) What is printed as the result of executing the following program?

    public class P20 {
      public static void main(String[] args) {
        int[] array = { 0, 1, 2, 3 };
        int k = 2;
        print(array, k);
        k = fiddle(array, k);
        print(array, k);
        k = fiddle(array, k);
        print(array, k);
      public static int fiddle(int[] a, int idx) {
        a[idx] = 3 * a[idx - 1];
        return a[idx] - 2;
      public static void print(int[] v, int j) {
        for (int i = 0; i < v.length; i++)
          System.out.print(v[i] + ", ");

    You should explain why the output is produced.

    9) Write a class OysterCard (with suitable instance variables) simulating a simplified pay-as-you-go Oyster card for the Tube.

    Each Oyster card stores its number and its current balance (in pounds). Implement a constructor with the card number (of type int) as its argument. Also, implement instance accessor methods getCardNumber() and getBalance().

    10) In the context of Question 9, implement instance method topUp(double amount). The method should check its argument and return false if the amount is negative; otherwise, it should increase the current balance by the given amount and return true.

    11) In the context of Question 9, override the instance method toString from class Object and provide a suitable representation for instances of OysterCard, including the card number of and its current balance


    12) Suppose class Station is declared as follows:
    public class Station {
      public int getZone() { ... }
    In other words, class Station has an instance method int getZone() that returns the zone the station is in.

    Implement instance method touch(Station station) in class OysterCard (see Questions 9-11), which is invoked when the user either `touches in' at the start of a journey or `touches out' at its end. At the start of a journey, the method should check the current balance and return false is it is below £5.00; otherwise, it should store the touch-in zone of the journey and return true. At the end of the journey, the method should adjust the balance according to the following table:

    touch in/out zone
















    Then, the method should record the fact that the journey has finished and return true.

    Hint: Use instance variable touchInZone to store either the touch-in zone (1, 2 or 3) of a journey or 0 to indicate that the journey has finished, but a new one has not started yet.

    13) Implement a method
    public static void printSubstrings(String word)
    that prints all symmetric non-empty substrings obtained by removing the first n and the last n characters from a given
    word, where n ranges from 0 to word.length()/2. The substrings should be aligned so that the same character of word appears in the same column. For example, if word is "elephant", then the method should print the following substrings (arranged as indicated):

    (Note the leading spaces.)

    Hint: Use instance method String substring(int s, int e) of class String to extract the substring that begins at index s and extends to the character at index e - 1.

    14) What is printed as the result of executing the following program?
    public class T20 {
      private int n, k;
      private static int v = 0;
      public T20(int i) {
        k = i;
        n = v;
      public static void main(String[] args) {
        T20 t1 = new T20(3);
        System.out.println(t1.k + "/" + t1.v + "/" + t1.n);
        T20 t2 = new T20(2);
        System.out.println(t2.k + "/" + t2.v + "/" + t2.n);
        T20 t3 = new T20(1);
        System.out.println(t3.k + "/" + t3.v + "/" + t3.n);

    Show your workings. How many instances of class T20 does the program create? Which of the statements has no effect on its printed output?

    Java Assignment Help, Programming Assignment Help UK, Java programming help, Software assignment help, Advanced programming homework help, Procedural programming Australia assignment help, C# Assignment Help, Operating System Assignment Help