Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Build a JavaFX Application Build a Pomodoro App Build the State Change Events

Christopher Mlalazi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 Points

Why have several pages

Having gone so far in the java tutorials as a beginner, one thing that is still confusing me is why we have several pages in a java project.

Of course I understand from HTML that we also need CSS and Javasceipt for page styling and interactivity - but here in java what is the order of these several pages, which is the main one which the others bring control to with their code?

And how do you decide that I need a new page/file to write extra code on etc?

I would have wished a lesson where first we start with an exhibition of only one file and then we see other files gradually added to the first one and wth explanations why we are adding these new files and what they do to the project. Here in this javaFx project we downloaded and imported files that were already prepared ....and here lies my problem as I would have wanted to see how they were prepared.

To cut a long story short, my question still remains, why do we have several pages like AttemptKind, Homefx, etc, and how do I decide that I need them?

4 Answers

stjarnan
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
stjarnan
Front End Web Development Techdegree Graduate 56,488 Points

Hi Chris!

There are a couple of reasons for this, one of them is something called 'separation of concern'. For an example, you have a program with a couple of classes doing different things, you could put them in different files with names explaining what the file contains. When the program gets bigger this is much easier to maintain and make changes to, when you know where to find what you're looking for.

This way every file gets less code and becomes easier to read.

So when do you create a new file? Well, keeping your classes in separate files is a great start. Here's a link explaining things further. The link leads to a C# question but I think the same principles matter.

https://www.reddit.com/r/csharp/comments/41bajj/noob_question_about_using_multiple_cs_files/

Hope this helped!

stjarnan
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
stjarnan
Front End Web Development Techdegree Graduate 56,488 Points

Oh yeah I totally forgot the link, edited it in!

If classes are the same in Java and C#, you could see them as containers of code. A common example here at Treehouse has been cars, so I will use that one. Imagine that you want to create a couple of cars, you create each car by creating variables like this:

var car1Type = "Coupe"; var car1Color = "red";

And you keep doing this until you feel like you've got all the properties you need. The thing is you need to do this for ALL cars you create, and you're repeating yourself a lot! Let's see how this could be done with a class:

class Car { var year; var make; var speed;

    public Car(yourCarsMake, yourCarsYear, YourCarsSpeed)
    {
        this.year = yourCarsYear;
        this.make = yourCarsMake;
        this.speed = YourCarsSpeed;
    }

}

Using this you can create how many new cars you like by then doing like this:

Car MyCar = new Car("Ford", 1993, "200km/h");

You repeat yourself much less than if you would have written hundreds of properties over and over again. Do note that the syntax in my variable examples isn't Java or C# but hopefully you get the idea, I am in a hurry and wanted to get the reply out as fast as possible for you :)

Christopher Mlalazi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 Points

Hey @stjarnan thanks I get it. It might have been explained in the earlier videos as the term separation of concerns rings a bell, and maybe I was too new to java to notice it then. I plan to revise the whole track since I am now almost at the end, maybe second time around I will begin to understand more. You mention a link but I think you forgot to put in one. I really appreciate this my buddy you are a sport. I have one further question, what is the purpose of a class and what does it really do? This is still not yet clear to me at this stage.