Question

Below I have my 3 files. I am trying to make a dog array that aggerates...

Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working.

//Main File

import java.util.*;
import java.util.Scanner;
public class Main
{
   public static void main(String[] args)
   {
   System.out.print("There are 5 humans.\n");
   array();
  
   }
   public static String[] array()
   {
  
   //Let the user enter the names of the humans
   String[] humans= new String[5];
   Scanner input = new Scanner(System.in);
   System.out.print("Enter the " + humans.length + " names: ");
   for (int i = 0; i < humans.length; i++)
   {
   humans[i] = input.nextLine();
   }
  

   for(int i=0;i<humans.length; i++)
   {
   System.out.print(humans[i]+" ");
   }
   return humans;
   }
}

//Human file

import java.util.*;
import java.util.Scanner;

public class Human
{
Dog [] dogs;
   public static void main(String[] args)
   {

   }
   public Human()
   {
   int numDogs = Dog.random();
   dogs = new Dog[numDogs];
  
   for(int i=0; i<dogs.length; i++)
   {
   dogs[i] = new Dog();
   }
  
   for(int i=0;i<dogs.length; i++)
   {
   System.out.print(dogs[i]+" ");
   }
     
   }   
}

//Dog file

import java.util.*;
import java.util.Scanner;

public class Dog
{
   public static void main(String[] args)
   {
  
   }
   public static int random()
   {
   int num = (int) (Math.random());
   String[] dogs = new String[num];
   return num;
   }
   public Dog()
   {

  
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

public class Main {

    public static void main(String[] args) {
        System.out.print("There are 5 humans.\n");
        
        // Let the user enter the names of the humans
        Human[] humans = new Human[5];
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the " + humans.length + " names: ");
        for (int i = 0; i < humans.length; i++) {
            humans[i] = new Human(input.nextLine());
        }

        for (int i = 0; i < humans.length; i++) {
            humans[i].print();
        }
        input.close();
    }
}

class Human {
    String name;
    Dog[] dogs;

    public Human(String name) {
        this.name = name;
        int numDogs = (int) (1 + 5 * Math.random());
        dogs = new Dog[numDogs];

        for (int i = 0; i < dogs.length; i++) {
            dogs[i] = new Dog();
        }
    }

    public void print() {
        System.out.println("Name: " + name);
        
        System.out.print("Dogs: ");
        for (int i = 0; i < dogs.length; i++) {
            System.out.print(dogs[i].getName() + " ");
        }
        System.out.println();
    }
}

class Dog {
    String name;
    
    Dog() {
        String names[] = {"Ace", "Apollo", "Bailey", "Bandit", "Baxter", "Bear", "Beau", "Benji",
                "Benny", "Bentley", "Blue", "Bo", "Boomer", "Brady", "Brody", "Bruno", "Brutus", "Bubba",
                "Buddy", "Buster", "Cash", "Champ", "Chance", "Charlie", "Chase", "Chester", "Chico"};
        name = names[(int) (Math.random() * names.length)];
    }

    public String getName() {
        return name;
    }
    
}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
Below I have my 3 files. I am trying to make a dog array that aggerates...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • The files provided in the code editor to the right contain syntax and/or logic errors. In...

    The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. // DebugFive2.java // Decides if two numbers are evenly divisible import java.util.Scanner; public class DebugFive2 { public static void main(String args[]) { int num; int num2; Scanner input = new Scanner(System.in); System.out.print("Enter a number "); num = input.nextInteger() System.out.print("Enter another number ");...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Computer Science - Java Explain the code step by step (in detailed order). Also explain what...

    Computer Science - Java Explain the code step by step (in detailed order). Also explain what the program required. Thanks 7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...

  • Please make the following code modular. Therefore contained in a package with several classes and not...

    Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q {    public static LinkedList<String> dogs = new LinkedList<String> ();    public static LinkedList<String> cats = new LinkedList<String> ();    public static LinkedList<String> animals = new LinkedList<String> ();    public static void enqueueCats(){        System.out.println("Enter name");        Scanner sc=new Scanner(System.in);        String name = sc.next();        cats.addLast(name);        animals.addLast(name);    }   ...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • I have a program that reads a file and then creates objects from the contents of...

    I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT