-1

This is a bit involved, and I want to explain this succinctly without making you read a lot, then show the code. I'm not good at that.

Barebones explanation. In this assignment, we are learning, how to compile,interpret in comand prompt, create a package, create a class and sub-classes, import the sub-classes, and execute println commands in all the sub-classes as a single compiled program, and display such in command prompt. I'm missing something, and the subclass println commands don't display when I run GreetingsClass.java, the Superclass. They are all in the same package. The package directory is com.cisp2030.course, and the three Chapters class files exist in a .Chapters folder inside of .course.

First, GreetingsClass.java:

package com.cisp2030.course;
import com.cisp2030.course.Chapters.*;

public class GreetingsClass
{
    Chapter1 c1 = new Chapter1();
    Chapter2 c2 = new Chapter2();
    Chapter3 c3 = new Chapter3();

    public static void main(String[] args)
    {
    System.out.println("$ Greetings, CISP2030!");
    System.out.println(c1);
    }

}

This is supposed to import and instantiate the variables of the next code contained as Chapter1.class in .Chapters folder.

package com.cisp2030.course.Chapters;
public class Chapter1
{
    public Chapter1()
    {
        System.out.println("Hello from Chapter1!");
    }
}

Just imagine that the above code is one of three that range from Chapter1-Chapter3, and they have been compiled by Command Prompt into class files in their respective directory.

The expected output of compiling, interpreting, and running this program in command prompt should be one program which displays all 3-4 println commands. However, at this time, running hte command prompt only displays the one println command. I think this is because I need to sub-class the Chapter classes to GreetingsClass, and having them imported already, somehow direct GreetingsClass to execute the commands of hte Chapters class files, but I don't know how, and I've googled this consistently, and searched through my textbook and am none the wiser. I think I'm missing something in the code itself, but I don't know enough to come up with any ideas. Any help or advice would be greatly appreciated.


Code has been finished:

package com.cisp2030.course;
import com.cisp2030.course.Chapters.*;

public class GreetingsClass
{

    public static void main(String[] args)
    {
    System.out.println("$ Greetings, CISP2030!");
    Chapter1 c1 = new Chapter1();
    Chapter2 c2 = new Chapter2();
    Chapter3 c3 = new Chapter3();
    }

}
  • With my instructor's direction, this has been resolved. The error I was getting declared I could not run a non-static command as static, this is because System.out.println(c1); was inside of the main method and labeled as static, but the Chapter1 c1 = new Chapter1 was above the main method, and thus was not set as static by the method below it. An error resulted. I had to set the three instantiation declarations under the println command, and delete the second print line that was there, and those three lines did what println tried to but could not for the other class files. – Steven Dorsey Feb 26 '15 at 02:41

1 Answers1

0

Does that even compile?

Java doesn't allow static constructors.

public Chapter1() // removed "static"
{
    System.out.println("Hello from Chapter1!");
}

c1 is a member of GreetingsClass. In main() you are in a static method, not a method of GreetingsClass and so you can't access it's members. Most obvious solution is to add GreetingsClass greetings = new GreetingsClass(); to main and try to get c1 out of that.

John3136
  • 28,809
  • 4
  • 51
  • 69
  • Oh, you're right. The static variable of that constructor was not there originally. I was getting mixed signals in my error codes and thought I needed that there. I couldn't get my sublcasses to extend my superclass GreetingsClass.java, because it said something about it not being able to do so while they were unstatic, and i guess I put that in the wrong place. In any case, the original code reads without static. And when I attempted to compile it with static in there, it does fail to compile, saying that that declaration can not be static. – Steven Dorsey Feb 20 '15 at 22:32