简体   繁体   中英

A class within a public class and creating a new object access to different variables

I've failed to find how to get a class within a public class. There is no syntax errors. The error comes from the receiving the methods for the class.

I am using Dr. Java to run this. I've tried to make a separate file attachments suggested in other user solutions, but the version I have does not have the tools in the other user versions. Please let me know how to achieve this.

public class TestLab2Bronze {
  public void main(String[] args) {
    //Create the teams in the Central division of the NHL.
    //This is intentionally very simple brute-force code.
    HockeyTeam team1 = new HockeyTeam("Winnipeg",22,14,8,"=",0);
    HockeyTeam team2 = new HockeyTeam("Chicago",28,13,2,"=",0);
    HockeyTeam team3 = new HockeyTeam("Colorado",18,17,8,"=",0);
    HockeyTeam team4 = new HockeyTeam("St. Louis",27,13,3,"=",0);
    HockeyTeam team5 = new HockeyTeam("Dallas",19,16,7,"=",0);
    HockeyTeam team6 = new HockeyTeam("Minnesota",18,19,5,"=",0);
    HockeyTeam team7 = new HockeyTeam("Nashville",29,9,4,"=",0);

    //Print out all 7 objects, again with simple code.
    System.out.println("Initial teams:\n" +
              team1 + "\n" + team2 + "\n" + team3 + "\n" + team4 + "\n" +
              team5 + "\n" + team6 + "\n" + team7 + "\n");

    //Record the results of some fictional games
    team1.won(); team2.lost();          //Winnipeg beat Chicago
    team1.won(); team3.lostOvertime();  //Winnipeg beat Colorado
    team1.won(); team4.lostOvertime();  //Winnipeg beat St. Louis
    team1.won(); team5.lost();          //Winnipeg beat Dallas
    team1.won(); team6.lost();          //Winnipeg beat Minnesota
    team1.won(); team7.lostOvertime();  //Winnipeg beat Nashville
    //OK. So the Jets are really hot right now.
    team4.won(); team2.lost();          //St. Louis beat Chicago
    team3.won(); team5.lost();          //Colorado beat Dallas   

    //Print out the 7 objects again, to see the changes.
    System.out.println("Final teams:\n" +
              team1 + "\n" + team2 + "\n" + team3 + "\n" + team4 + "\n" +
              team5 + "\n" + team6 + "\n" + team7 + "\n");
  }
}

class HockeyTeam {
  public int point;

  public HockeyTeam(String city, int scoreone, int scoretwo, int scorethree, String equals, int point) {
    this.point = point;
  }

  public void won() {
    point += 2;
  }

  public void lost() {
    point += 0;
  }

  public void lostOvertime() {
    point += 1;
  }

  public int points() {
    return point;
  }
}

Check the location of class. If you are using 8n same pkg there is no issue. It will issue if there is different path of classess. At that time you need to set classpath od calling class. Kindly swt the class path of the file and then you can use it with current class

main method must be static:

public static void main(String[] args)

HockeyTeam should be in Test5Lab2Bronze class, static and be declared as following:

public static class HockeyTeam

code should look like this:

public class TestLab2Bronze {

    public static void main(String[] args) {
        // Create the teams in the Central division of the NHL.
        // This is intentionally very simple brute-force code.
        HockeyTeam team1 = new HockeyTeam("Winnipeg", 22, 14, 8, "=", 0);
        HockeyTeam team2 = new HockeyTeam("Chicago", 28, 13, 2, "=", 0);
        HockeyTeam team3 = new HockeyTeam("Colorado", 18, 17, 8, "=", 0);
        HockeyTeam team4 = new HockeyTeam("St. Louis", 27, 13, 3, "=", 0);
        HockeyTeam team5 = new HockeyTeam("Dallas", 19, 16, 7, "=", 0);
        HockeyTeam team6 = new HockeyTeam("Minnesota", 18, 19, 5, "=", 0);
        HockeyTeam team7 = new HockeyTeam("Nashville", 29, 9, 4, "=", 0);

        // Print out all 7 objects, again with simple code.
        System.out.println("Initial teams:\n" + team1 + "\n" + team2 + "\n"
                + team3 + "\n" + team4 + "\n" + team5 + "\n" + team6 + "\n"
                + team7 + "\n");

        // Record the results of some fictional games
        team1.won();
        team2.lost(); // Winnipeg beat Chicago
        team1.won();
        team3.lostOvertime(); // Winnipeg beat Colorado
        team1.won();
        team4.lostOvertime(); // Winnipeg beat St. Louis
        team1.won();
        team5.lost(); // Winnipeg beat Dallas
        team1.won();
        team6.lost(); // Winnipeg beat Minnesota
        team1.won();
        team7.lostOvertime(); // Winnipeg beat Nashville
        // OK. So the Jets are really hot right now.
        team4.won();
        team2.lost(); // St. Louis beat Chicago
        team3.won();
        team5.lost(); // Colorado beat Dallas

        // Print out the 7 objects again, to see the changes.
        System.out.println("Final teams:\n" + team1 + "\n" + team2 + "\n"
                + team3 + "\n" + team4 + "\n" + team5 + "\n" + team6 + "\n"
                + team7 + "\n");

    }// main

    public static class HockeyTeam {
        public int point;

        public HockeyTeam(String city, int scoreone, int scoretwo,
                int scorethree, String equals, int point)

        {
            this.point = point;

        }

        public void won()

        {
            point += 2;

        }

        public void lost()

        {
            point += 0;

        }

        public void lostOvertime()

        {
            point += 1;

        }

        public int points()

        {
            return point;

        }

    }

}

Yes I've changed the Hockey class location and set it to static and it runs find, except it does not print out the values in text language form (readable).

Welcome to DrJava. Working directory is C:\\Users\\Acer

run TestLab2Bronze Initial teams: TestLab2Bronze$HockeyTeam@67dd9e TestLab2Bronze$HockeyTeam@143b388 TestLab2Bronze$HockeyTeam@1f2e781 TestLab2Bronze$HockeyTeam@14b9a40 TestLab2Bronze$HockeyTeam@1842d5a TestLab2Bronze$HockeyTeam@1bd1811 TestLab2Bronze$HockeyTeam@1ed3c88

Final teams: TestLab2Bronze$HockeyTeam@67dd9e TestLab2Bronze$HockeyTeam@143b388 TestLab2Bronze$HockeyTeam@1f2e781 TestLab2Bronze$HockeyTeam@14b9a40 TestLab2Bronze$HockeyTeam@1842d5a TestLab2Bronze$HockeyTeam@1bd1811 TestLab2Bronze$HockeyTeam@1ed3c88

I would like to print:

Initial teams: Winnipeg(22,14,8=52) Chicago(28,13,2=58) Colorado(18,17,8=44) St. Louis(27,13,3=57) Dallas(19,16,7=45) Minnesota(18,19,5=41) Nashville(29,9,4=62)

Final teams: Winnipeg(28,14,8=64) Chicago(28,15,2=58) Colorado(19,17,9=47) St. Louis(28,13,4=60) Dallas(19,18,7=45) Minnesota(18,20,5=41) Nashville(29,9,5=63)`

Same spacing as the original just the text to be readable. Thank you for your suggestions and solutions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM