0% found this document useful (0 votes)
26 views2 pages

Java Programming Assignment: Code Tasks

The document provides 7 programming assignments involving static methods and blocks in Java. Students are asked to code, compile, test, and note the output of programs demonstrating static variable scoping, pass by value vs reference, recursion, and when static blocks are executed. Common tasks include fixing compilation errors, swapping values, printing arrays, and implementing recursive methods to calculate things like digits and triangular numbers.

Uploaded by

Ani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Java Programming Assignment: Code Tasks

The document provides 7 programming assignments involving static methods and blocks in Java. Students are asked to code, compile, test, and note the output of programs demonstrating static variable scoping, pass by value vs reference, recursion, and when static blocks are executed. Common tasks include fixing compilation errors, swapping values, printing arrays, and implementing recursive methods to calculate things like digits and triangular numbers.

Uploaded by

Ani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment -3

1. Code and compile following :


public class Static {
private String name = "Static class";
public static void first() { }
public static void second() { }
public void third() { [Link](name); }
public static void main(String args[]) {
first();
second();
third();
}}

Observe the compilation error and try to fix the error. Corrected code is to be included in file.

2. Code and compile both class definitions


First Code
class calltest{
public static void main (String a[]){
String s=new String("English");
fun(s);
[Link](s);
}
static void fun(String s){
[Link](s);
s="Hindi";
[Link](s);
}}

Second Code

class intc{
int x;
} class reftest{
public static void main (String a[]){
intc o=new intc();
fun(o);
[Link](o.x);
}
static void fun(intc s){
[Link](s.x);
s.x=10;
[Link](s);
}}

Observe and NOTE the difference in the output behaviour.

3. Code and test the following , NOTE the output and justify it.
public class Percolate {
public static void main (String[] args) {
int[] dataSeq = {6,4,8,2,1};
printIntArray(dataSeq);
for (int index = 1; index < [Link]; ++index)
if (dataSeq[index-1] > dataSeq[index])
swap(dataSeq, index-1, index);
printIntArray(dataSeq);
}
public static void swap(int[] intArray, int i, int j) {
int tmp = intArray[i]; intArray[i] = intArray[j]; intArray[j] = tmp;
}
public static void swap(int v1, int v2) {
int tmp = v1; v1 = v2; v2 = tmp;
}
public static void printIntArray(int[] array) {
for (int value : array)
[Link](" " + value);
[Link]();
}
}

4. Code and test the following , NOTE the output and justify it.

public class ParameterPass {


public static void main(String[] args) {
int i = 0;
addTwo(i++);
[Link](i);
}s
tatic void addTwo(int i) {
i += 2;
}}

5. Write and test the following method that returns digit number k of the positive integer n:
static int digit(long n, int k)
For example, digit ( 8 6 4 2 1 , 3 ) would return 6, and digit ( 8 6 4 2 1 , 7 ) would return 0.

6. Write and test the following recursive method that returns the nth triangular number:
static long t(int n)
The triangular numbers are 0, 1,3,6, 10, 15,21,28, ... . Note that t(n) = t(n-1) + n for n > 1.

7. Write java program to demonstrate use of static block. Excersice to know when static
block is executed?

Common questions

Powered by AI

The method `digit(long n, int k)` returns the digit of number `n` at position `k`. Implementation involves converting `n` to a string, checking if `k` exceeds the length (return 0, default), and converting the character at position `length-k` to a digit, considering indexing from the right (least significant to most significant). This effectively extracts and returns the desired digit when valid .

The integer `i` remains `0` when printed after execution because Java passes method parameters by value, meaning a copy of `i` is passed to `addTwo()`. The post-increment operation `i++` in `addTwo()` increases the local copy by 2, but the original `i` in `main()` remains unaffected, resulting in the output '0' .

The recursive method `t(int n)` calculates triangular numbers using the relation `t(n) = t(n-1) + n` for `n > 1`. The recursion builds upon the known base case `t(0) = 0`, adding each number sequentially up to `n`, which results in the sum of integers from 1 to `n`. This recursive definition naturally aligns with the triangular number sequence, creating an efficient recursive accumulation .

The compilation error occurs because the `third()` method, which is non-static, is called from a static context in `main()`. To correct this, create an instance of the `Static` class to call `third()`, such as by using `new Static().third();` in `main()`. This works because `third()` needs an instance to access the instance field `name` .

When a `String` is passed to the method `fun`, altering its value within the method does not affect the original reference due to String immutability, resulting in `System.out.println(s)` printing 'English' after method call. Conversely, for `intc` object, the method `fun` changes an internal field (`x`), which affects the original object, hence `System.out.println(o.x)` outputs '10' after the call .

A static block in Java is executed once when the class is loaded into memory, before object creation and method execution. This provides a mechanism to initialize static variables or run setup code that needs to execute once. Its main benefit is ensuring essential static initialization, crucial for settings or constants necessary before instance creation or method calls .

The `Percolate` class implements a simple adjacent pair sorting within the `main()` method. It compares two consecutive elements of the `dataSeq` array and swaps them if the previous element is greater than the next, which is repeated through a loop over the array. This operation affects each pair once, effectively bubbling smaller elements earlier in the sequence. As a result, the array partially sorts, but only one iteration across the length results in `6,4,8,2,1` rearranging to `4,6,8,1,2` .

You might also like