0

this is my first question on here and I did a search before forming it, so I hope everything is as required.

I am working on a school assignment in Java. I am able to produce the required output but there are a lot of null instances created first. I don't understand why. Information about the library the professor created for the course and the code are below

Library included with this course: i2c.jar. It can be found here.

included in this Library are the classes Country and CountryDB. The API for the Country class can be found at http://130.63.94.24/~roumani/book/doc/i2c/ca/roumani/i2c/Country.html

The API for the CountryDB class can be found at http://130.63.94.24/~roumani/book/doc/i2c/ca/roumani/i2c/CountryDB.html

I am asked to create a class called Game, using the Country and CountryDB APIs.

The only attribute is db, which is an instance of CountryDB.

The constructor only sets the attribute (db) for this instance to a new CountryDB object.

The class is also meant to include a method (called qa) that follows this pseudocode:

  1. get a reference to the database's capital city list
  2. determine the size of this list. Cal it n.
  3. generate a random number in [0,n) called index.
  4. invoke get(index) on the list to get a random capital city. Call it c
  5. get a reference to the database's data map
  6. invoke get(c) on the map to get a reference to a country. Call it ref.

The method is then supposed to return one of two Strings (which will be clear in the code). Everything works as it should, except I get a lot of "nulls" before the desired output. When made into a List, db has size 241 so I suspect I am creating 241 null instances and 1 proper instance. I have no idea why though. I have tested every line of code in my method and the constructor was dictated by the textbook.

CODE

package ca.yorku.eecs.caps;

import java.util.List;
import java.util.Map;

import ca.roumani.i2c.Country;
import ca.roumani.i2c.CountryDB;

public class Game
{
    private CountryDB db;

    public Game()
    {
       this.db = new CountryDB();
    }

    public String qa()
    {
       List<String> capitals = db.getCapitals();
       System.out.println(capitals.toString());
       int n = capitals.size();
       System.out.println(n);
       int index = ((int) (n * Math.random()));
       System.out.println(index);
       String c = capitals.get(index);
       System.out.println(c);

       Map<String, Country> data = db.getData();
       Country ref = data.get(c);

       if (Math.random() > 0.5)
         {
           return "What is the capital of " + ref.getName() + "? \n" + ref.getCapital();
         }
       else
         {
           return ref.getCapital() + " is the capital of? \n" + ref.getName();
         }
    }

    public static void main(String[] args)
     {
        Game g = new Game();
        System.out.println(g.qa());
     }

}

the System.out.println() statements are only there to test when the nulls occur. It clearly happens immediately because my psvm output is 241 nulls (on separate lines) followed by my desired output. Can somebody please tell me what I am doing wrong?

And, more generally (to help more people) how do you implement classes, the constructor of which instantiates another class and sets it as an attribute value?

I appreciate any help. Also, please note, I am not trying to get others to do my work for me. I've spent hours on this and my lab TA also wasn't sure why it happens either. He would have helped me correct it had he known how.

Thank you.

Lavish Kothari
  • 2,211
  • 21
  • 29
  • I think the nulls that you are getting are probably printed by the API you are using. Probably `db.getCapitals()` is printing that stuff. – Lavish Kothari Nov 25 '18 at 03:31
  • What does y psvm mean ? Also which printout prints nulls ? – c0der Nov 25 '18 at 06:45
  • 1
    You need to learn how to debug your code. The least you should be able to do, is find out exactly which line in your code causes these null values to be printed. Then you can remove all code not needed to reproduce the issue until you have a true [MCVE]. Then we can help. – Max Vollmer Nov 25 '18 at 14:48
  • 1
    Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Max Vollmer Nov 25 '18 at 14:48
  • Max, I'm a beginner with a simple question for which I provided all the necessary information. I tested every line in the method (see all the print statements?) the nulls occur before anything in the method does, so it's clearly caused by the constructor or something to do with the CountryDB class. I was very clear in my question. If you don't want to help then move on c0der: psvm is public static void main as can be seen in my code. It's used to test my output. I post the required info and no one even cares to help, only edit my question. Thanks a lot.... – David Guilfoyle Nov 25 '18 at 19:17

0 Answers0