Object-Oriented Programming with Java, part I – Exercise 100

This exercise takes inputs of density, height and width and creates a random asterisks (star) field out of them.

My Code

Main

public class Main {

    public static void main(String[] args) {
        // Test your code here
        NightSky NightSky = new NightSky(0.1);
        NightSky.print();
        System.out.println("Number of stars: " + NightSky.starsInLastPrint());
        System.out.println("");

        NightSky.print();
        System.out.println("Number of stars: " + NightSky.starsInLastPrint());
    }
}

NightSky

import java.util.Random;

public class NightSky {

    private double density;
    private int width;
    private int height;
    private int starsInLastPrint;

    public NightSky(double density) {
        this.density = density;
        this.width = 20;
        this.height = 10;

    }

    public NightSky(int width, int height) {
        this.density = 0.1;
        this.width = width;
        this.height = height;

    }

    public NightSky(double density, int width, int height) {
        this.density = density;
        this.width = width;
        this.height = height;

    }

    public void printLine() {
        for (int i = 0; i < this.width; i++) {
            if (Math.random() >= this.density) {
                System.out.print(" ");
            } else {
                System.out.print("*");
                this.starsInLastPrint++;
            }

        }

    }

    public void print() {
        this.starsInLastPrint = 0;
        for (int i = 0; i < this.height; i++) {
            printLine();
            System.out.println("");
        }
    }
    
    public int starsInLastPrint(){
        return this.starsInLastPrint;
    }

}

Model Code

Main

public class Main {

    public static void main(String[] args) {
        // Test your code here
        NightSky NightSky = new NightSky(0.1);
        NightSky.print();
        System.out.println("Number of stars: " + NightSky.starsInLastPrint());
        System.out.println("");

        NightSky.print();
        System.out.println("Number of stars: " + NightSky.starsInLastPrint());
    }
}

NightSky

import java.util.Random;

 

public class NightSky {

 

    private double density;

    private int width;

    private int height;

    private int starsInLastPrint;

 

    public NightSky(double density) {

        this(density, 20, 10);

    }

 

    public NightSky(int width, int height) {

        this(0.1, width, height);

    }

 

    public NightSky(double density, int width, int height) {

        this.density = density;

        this.width = width;

        this.height = height;

    }

 

    public void print() {

        this.starsInLastPrint = 0;

 

        for (int y = 0; y < this.height; y++) {

            printLine();

        }

    }

 

    public void printLine() {

        Random random = new Random();

 

        for (int x = 0; x < this.width; x++) {

            double randomValue = random.nextDouble();

 

            if (randomValue > this.density) {

                System.out.print(" ");

            } else {

                System.out.print("*");

                this.starsInLastPrint = this.starsInLastPrint + 1;

            }

        }

 

        System.out.println("");

    }

 

    public int starsInLastPrint() {

        return starsInLastPrint;

    }

}

Comment

Both are pretty much the same. I am not sure why I had to initialise starsInLastPrint to 0 in the place that I did though which is a bit of a bugger.

Leave a comment