1. Given the following code, how many references exist to the Time object at the end of the main method and what are their names? public class Foo { public static void main(String args[]) { Time t = new Time(); Time t1 = t; Time t2 = t; Clock clock = new Clock(t); // AT THIS POINT, HOW MANY REFERENCES TO THE TIME OBJECT ARE THERE } } public class Time { } public class Clock { private Time time; public Clock(Time t) { time = t; } }
2. What does the following code display to the screen? public void run() { int num1 = 20; int numArray[] = {30,40,50,60,70}; MyMethod(num1); System.out.println(num1); MyMethod(numArray); System.out.println(numArray[2]); } public void MyMethod(int i) { i += 5; } public void MyMethod(int i[]) { for (int j = 0; j < i.length; j++) { i[j] += 5; } }
I have answered both the questions then please leave a comment I will get back to you.
1. In this program there is Only One object
i.e. t and three references that is:- t, t1 and
t2
where
2. Nothing will be displayed if the code is run as it is. As for java program the starting point is main()
Is you update the code and call make run from main() then the output would be:
20
55
and the logic behind this that first print statement is just printing the num1 which is not updated as in java arguments are always passed by value so num1 will not be updated after the call MyMethod(num1); and System.out.println(num1); will print 20
And in case of array this reference will be passed by value, which is copied. It will still point at the original array. So, References to objects are passed by value and hence the value updated in method MyMethod(int i[]) will be depicted in the print statement 55
1. Given the following code, how many references exist to the Time object at the end...
**JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args) for (int k-1: k10:k if (k8) continue: System.out.println k) Question 2 3.5 pts Analyze the following code. Which one is correct? package p public class A package p2: import p1. public class B extends Af int i- 5 private int j- 10: public A0I public static void main(Stringll args) B b new B0: System.out.println(a.i+", "+ajt", "+b.it""+bj): a.i is printable None of them b.i is...
c) public class Test { public static void main(String[] args) { T t1 = new T(); T t2 = new T(); System.out.println("t1's i = " + t1.i + " and j = " + t1.j); System.out.println("t2's i = " + t2.i + " and j = " + t2.j); } } class T { static int i = 0; int j = 0; T() { i++; j = 1; } } Why is t1's i = 2 ? It should be...
(a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) { System.out.println ("Hello"); x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) { sum = sum + i; i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...
please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...
how to call maze1 and maze2 object to be use in print method maze2.maze[i][j] maze2 is object maze[i][j] is variable from different class i just need help in calling object to be used in the print method and main will call print method to display. import java.io.*; public class MazeSolver { //=========================================== // object reader //========================================== public static void read()throws FileNotFoundException,IOException{ reader maze1 = new reader(new FileReader("maze1.txt")); reader maze2 = new reader(new...
Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...
The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...
I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass { public static void main(String[] args) { int a[] = {3, 2, 5, 6, 1}; InsertionSortClass insertion = new InsertionSortClass(); System.out.print("The original list : "); System.out.println(); insertion.printArray(a); System.out.println(); System.out.println("The list after insertionSort : "); System.out.println(); insertion.insertionSort(a); } } package DriverClass; public interface SortADTInterface { public void insertionSort(int arr[]); public void printArray(int arr[]); } package DriverClass; public class InsertionSortClass implements SortADTInterface{ public void insertionSort(int[] arr)...
The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...
the code needs to be modifed. require a output for the
code
Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...