Hi,
I'm having issues with driver class is giving me issues. The code needs 2 for loops requirement.
The driver class will create an array of 3 CircusClown objects.
Use a loop to get user input for your 3 objects.
Use a loop to print the toString() method of your 3 objects.
Determine and print the name of the clown that earned the most bonus pay for the week.
The code must work with ANY sized array. I said to make an array of size 3, but if you
were to change it to size 100, you should not have to alter ANY of your code.
Here is my work any help please.

// Class Clown
public class Clown {
    private String name;
    private double salary;
    private double hours;
    private double years;

    public Clown( String n, double s, double h, double y ) {

    name = n;
    salary = s;
    hours = h;
    years = y;
}

//getter methods
    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public double getHoursWorked() {
        return hours;
    }

    public double getYearsWorked() {
        return years;
    }

    public double calcBaseWeekPay() {
        return (hours * salary);
    }

    public double calcBonusAmount() {
        double rate = .05;

        if (years >= 30) {
            rate = .3;
        }
        else if (years >= 15 && years < 30) {
            rate = .15;
        }
        else {
            rate = .05;
        }
        return calcBaseWeekPay() * rate;
    }

    public double calcTotalPay() {
        return calcBaseWeekPay() + calcBonusAmount();
    }

    public String toString() {
        return "Clowns Name is" + name +
                "Bonus Amount is" + calcBonusAmount() +
                "Total Weekly Pay is" + calcBaseWeekPay();
    }
}

Here is my diver tester.

import java.util.*;

public class ClownTester {

    public static void main(String[] args) {

        Scanner play = new Scanner(System.in);

        String name;
        double salary;
        int hours;
        int years;

        for (int i = 0; i < circusClowns.length(); i++) {
            System.out.print("Enter the name of the clown: ");
            name = play.nextLine();
            System.out.print("Enter salary for " + name + ": ");
            salary = play.nextDouble();
            System.out.print("Enter the number of hours: ");
            hours = play.nextInt();
            System.out.print("Enter the number of years: ");
            years = play.nextInt();

            circusClowns[i] = new Clown(name, salary, hours, years);
        }

        //  Now, for the second for loop to print, just use the one you already have:
        for (Clown cc : circusClown) {
            System.out.println(cc.toString());
        }

        double mostBonusPay = circusClown[0].calcBonusAmount() //assume the biggest is the first one
                int index = 0; //assume the biggest is the first one

        for (int i = 1; i < circusClown.length(); i++) {
            if (circusClown[i].calcBonusAmount() > mostBonusPay) {
                mostBonusPay = circusClown[i].calcBonusAmount();
                index = i;
            }
        }
    }
}

Recommended Answers

All 14 Replies

you need to declare circusClowns somewhere in your driver class.

Clown[] circusClowns = new Clown[number of clowns that I want];

you could make the user ask for the number of clown that it wants first and foremost, and then go with the loop that asks information regarding a specific clown.

I would advice you declare the circusClowns array inside the main method.

Hi thanatos2,

you mean this?

System.out.println("How many positions do you want?");
userInput = play.nextInt();
Clown[] circusClowns = new CircusClown[userInput];

Where exactly I will insert the lines please.
Thks

insert that at line 13 in your driver class.

Thank you for you help, but I still complaint int this lines; 14,15,18,33,37,40,41,42. Here is the new update driver class.

import java.util.*;

public class ClownTester {

    public static void main(String[] args) {

        Scanner play = new Scanner(System.in);

        String name;
        double salary;
        int hours;
        int years;
        System.out.println("How many positions do you want?");
        userInput = play.nextInt();
        Clown[] circusClowns = new CircusClown[userInput];


        for (int i = 0; i < circusClowns.length(); i++) {
            System.out.print("Enter the name of the clown: ");
            name = play.nextLine();
            System.out.print("Enter salary for " + name + ": ");
            salary = play.nextDouble();
            System.out.print("Enter the number of hours: ");
            hours = play.nextInt();
            System.out.print("Enter the number of years: ");
            years = play.nextInt();

            circusClowns[i] = new Clown(name, salary, hours, years);

        }

        //  Now, for the second for loop to print, just use the one you already have:
        for (Clown cc : circusClown) {
            System.out.println(cc.toString());
        }

        double mostBonusPay = circusClown[0].calcBonusAmount(); //assume the biggest is the first one
                int index = 0; //assume the biggest is the first one

        for (int i = 1; i < circusClown.length(); i++) {
            if (circusClown[i].calcBonusAmount() > mostBonusPay) {
                mostBonusPay = circusClown[i].calcBonusAmount();
                index = i;
            }
        }
    }
}

instead of:

userInput = play.nextInt();

write:

int userInput = play.nextInt();

Thanks, but I still have issues with the other lines.

it would help if you told me what issues you have....

copy and paste this code:

import java.util.*;

public class ClownTester {

    public static void main(String[] args) {

        Scanner play = new Scanner(System.in);

        String name;
        double salary;
        int hours;
        int years;
        System.out.println("How many positions do you want?");
        int userInput = play.nextInt();
        Clown[] circusClowns = new Clown[userInput];


        for (int i = 0; i < circusClowns.length; i++) {
            System.out.print("Enter the name of the clown: ");
            name = play.nextLine();
            System.out.print("Enter salary for " + name + ": ");
            salary = play.nextDouble();
            System.out.print("Enter the number of hours: ");
            hours = play.nextInt();
            System.out.print("Enter the number of years: ");
            years = play.nextInt();

            circusClowns[i] = new Clown(name, salary, hours, years);

        }

        //  Now, for the second for loop to print, just use the one you already have:
        for (Clown cc : circusClowns) {
            System.out.println(cc.toString());
        }

        double mostBonusPay = circusClowns[0].calcBonusAmount(); //assume the biggest is the first one
        int index = 0; //assume the biggest is the first one

        for (int i = 1; i < circusClowns.length; i++) {
            if (circusClowns[i].calcBonusAmount() > mostBonusPay) {
                mostBonusPay = circusClowns[i].calcBonusAmount();
                index = i;
            }
        }
    }
}
commented: We do not do people's homework for them -3

Thanks,

Here is the errors I'm getting. "error" it means the error from eclipse complaint.

for (int i = 0; i < circusClowns.length(); i++) {
error: Cannot invoke length() on the array type Clown[]

for (Clown cc : circusClown) {
error: circusClown cannot be resolved to a variable

double mostBonusPay = circusClown[0].calcBonusAmount(); //assume the biggest is the first one
error: circusClown cannot be resolved to a variable


for (int i = 1; i < circusClown.length(); i++) {
            if (circusClown[i].calcBonusAmount() > mostBonusPay) {
                mostBonusPay = circusClown[i].calcBonusAmount();
error: circusClown cannot be resolved to a variable             

look at my answer above.

thanatos2, I really appreciate you help, I'm new to java.

There is two syntax errors;

Scanner play = new Scanner(System.in);
error:Resource leak: 'play' is never closed

int index = 0; //assume the biggest is the first one
error: The value of the local variable index is not used

As far as I can see, your driver is asking for doubles and your main class is sending ints. So thats never going to work. You need to have

public Clown( String n, double s, int h, int y )

in your Clown class to match your current imput (and change everything else to match ints in that class) or

 for (int i = 0; i < circusClowns.length; i++) {
            System.out.print("Enter the name of the clown: ");
            name = play.nextLine();
            System.out.print("Enter salary for " + name + ": ");
            salary = play.nextDouble();
            System.out.print("Enter the number of hours: ");
            hours = play.nextDouble();
            System.out.print("Enter the number of years: ");
            years = play.nextDouble();

otherwise your going to have all kinds of errors. If you fix that then it will be a lot easier to start debugging the rest of your program. I hope that helps!

        System.out.println("How many positions do you want?");
        int userInput = play.nextInt(); // this will lead to problems
        Clown[] circusClowns = new Clown[userInput];
        for (int i = 0; i < circusClowns.length; i++) {
            System.out.print("Enter the name of the clown: ");
            name = play.nextLine();

this part of the posted code will lead to new errors , as the nextInt() method will leave behind a newline character which will be picked up by the nextLine() method , and the program will crash. put an extra play.nextLine(); right below the for() and before the System.out.print("Enter the name of the clown: "); part to catch the stray newline character left in the stream.

As far as I can see, your driver is asking for doubles and your main class is sending ints. So thats never going to work.

seems to work for me.. promotion into a higher byte variable is done automatically , however , if it was a double to int conversion , that would result in an error.
that said, just because java takes care of int to double conversion , we shouldn't knowingly pass int to somthing that asks for a double either, especially when we can do something about it.

Thank you, guys for your tremendous help. It works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.