Search Logic Blocks

Saturday, February 1, 2020

Java: Let's work on some problems

Problem: Using program in earlier post Classes and Objects, add two more class variables for name of dog and type of dog. Modify the constructor to set the values. Create one method to access all the details about the dog.
// Dog.Java

Class Dog (Dog.java)
import java.awt.*;

public class Dog {
    String name;
    String type;
    int age;
    int size;
    Color color;
    String colorName;

    Dog(String name, String type, int age, int size, Color color) {
        this.name = name;
        this.type = type;
        this.age = age;
        this.size = size;
        this.color = color;
    }

    public int getAge() {
        return this.age;
    }

    public void bark() {
        System.out.println("Bark - woof, woof");
    }

    public Color getColor() {
        return this.color;
    }

    public void getDogDetails() {
        System.out.println("Dog's Details:");
        System.out.println("Name: " + name);
        System.out.println("Type: " + type);
        System.out.println("Age : " + age);
        System.out.println("Size: " + size);
    }
}
// DogTest.java
Class DogTest (DogTest.java)
import java.awt.*;

public class DogTest {
    public static void main(String args[]) {
        Dog dog1 = new Dog("Tommy", "Bulldog",3, 4, Color.WHITE);

        dog1.getDogDetails();
        dog1.bark();
    }
}
Problem: In the example given in the post Static Variables, suppose we have to keep a counter how many kids have age greater than 8. Write a full program with an example output.
// Student.java
Class Student (Student.java)

public class Student {
    String name;
    int age;
    static int total = 0;
    static int gr8Total = 0;

    Student(String name, int age) {
        this.name = name;
        this.age = age;
        total++;
        if(age >= 8) gr8Total++;
    }
}
// StudenTest.java
Class StudentTest (StudentTest.java)
public class StudentTest {
    public static void main(String args[]) {
        Student s1 = new Student("John", 6);
        Student s2 = new Student("Adam", 9);
        Student s3 = new Student("Alex", 7);
        System.out.println("Student1 -");
        System.out.print("Name  : " + s1.name + ", ");
        System.out.print("age   : " + s1.age + ", ");
        System.out.println("");

        System.out.println("Student2 -");
        System.out.print("Name  : " + s2.name + ", ");
        System.out.print("age   : " + s2.age + ", ");
        System.out.println("");

        System.out.println("Student3 -");
        System.out.print("Name  : " + s3.name + ", ");
        System.out.print("age   : " + s3.age + ", ");
        System.out.println("");

        System.out.print("Total Students Having Age Greater Than 8 - ");
        System.out.println(Student.gr8Total);
    }
}
// Output
Student1 -
Name  : John, age   : 6, 
Student2 -
Name  : Adam, age   : 9, 
Student3 -
Name  : Alex, age   : 7, 
Total Students Having Age Greater Than 8 - 1
Problem: When you try to call a non-static method from the static method, what kind of error do you get?

Solution: The program will give compilation error. - "java: non-static method <method> cannot be referenced from a static context"

Problem: Create an array of 6 double values and print them backwards.

Class ArrBack (ArrBack.java)
public class ArrBack {
    public static void main(String args[]) {
        double nums[] = {11.5, 3.2, 9.1, 11.80, 20.23, 70.76};

        for(int i = (nums.length-1); i >= 0; i--) {
            System.out.println("nums[" + i + "]: " + nums[i]);
        }
    }
}
// Output
nums[5]: 70.76
nums[4]: 20.23
nums[3]: 11.8
nums[2]: 9.1
nums[1]: 3.2
nums[0]: 11.5
Problem: Change the class ArrTest given in the post for-each loop to compute the average of the elements of the array. 

Class Average (Average.java)
public class Average {
    public static void main(String args[]) {
        int nums[] = {11, 3, 7, 8, 20, 16};
        int sum = 0;
        int size = nums.length;

        for(int num: nums) {
            sum = sum + num;
        }

        double avg = sum / size;
        System.out.println("Average is " + avg);
    }
}
// Output
Average is 10.0
Problem : Write a Java program to print all the numbers between 1 and 100, which are divisible by 3 or 7 or by both.

Solution :

Class DivBy3N7 (DivBy3N7.java)
public class DivBy3N7 {
    public static void main (String args[]) {
        for(int i = 1; i <= 100; i++) {
            if((i % 21) == 0) {
                System.out.print(i + " ");
            } else if((i % 3) == 0) {
                System.out.print(i + " ");
            }
            else if((i % 7) == 0) {
                System.out.print(i + " ");
            }
        }
    }
}
Output : 
3 6 7 9 12 14 15 18 21 24 27 28 30 33 35 36 39 42 45 48 49 51 54 56 57 60 63 66 69 70 72 75 77 78 81 84 87 90 91 93 96 98 99 

No comments: