Register   Login   About   Study   Enterprise   Share
Internet / AI Technology University (ITU/AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 1. Java Introduction
Current Topic: 1.2.3 Java Class Structure
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
You just wrote your first program. And it was easy, right? You might be slightly disappointed: this is called programming? My dog can do this!
I do not want to disillusion you. Yes, programming consists of simple elements similar to what you have just done. This is what computers understand: small and simple pieces of code, but real tasks include a number of these elements. The art is in planning how to break down a business idea into these small elements. We came to the heart of programming: planning or designing a program with the following implementation or coding.

Source consists of code and comments.
Code is written for compilers - programs that prepare code for running on a computer.
Comments are written for people, helping to understand code.
Comments also called "headers".
Comments are separated from the code by special patterns:
/** - beginning of the comments

- ending of the comments */

Everything inside these patterns - for people, not for compilers.
It is an important responsibility of a programmer - to write good comments, explain the plan and implementation of a program.

This leads us to the structure of any Java program. Start the program with two-three paragraph plan captured as a header of a program. Here is the description of the program goals, written in plain English. Even if your native language is different, stick with English to make your program readable by Java programmers. Remember that Java is an English-based programming language.
The header of the program starts with /** and ends with */ characters.

By the way, these starting and ending sequences tell a Java compiler that this was written for people, not for a machine, and can serve as readable documentation. Java tools include JavaDoc, the tool that creates nice HTML pages out of the headers. So, by providing these headers, you will be able to kill at least two birds with one stone by creating both a plan as well as a documentation.

Java class starts with the name of the class and then you describe data that is the essence of the class. Keep data private! No one should directly access and change the data. Only your methods should manipulate data, get data, set data, or calculate data. You make methods public to allow other classes to touch the data via the public methods.



ClassStructure
Universal Modeling Language (UML) allows us to illustrate code.
In UML a class looks like a rectangle with three main parts: class name, data, and methods.
Usually we introduce public methods after private data. This is the most common style of Java programming. Here is an Example of a Java class. The class starts with its name and is followed with private data that belong to this class and become part of any object instantiated from the class blueprint. Then, there are two public methods: getQuestion and setQuestion to manage the private data String question.

The last method, main is usually to start and test the class. When we order Eclipse Run As - Java Application, Eclipse will start the main method.

In the main method, we usually create an object of a class by using the new keyword:


Example example = new Example(); // Example is the name of the class and example is the name of an object of this class


Then, we use this object to change data of the class by calling the methods:


example.setQuestion("What is Java?"); // setting data
String questionInExample = example.getQuestion(); // getting data into a String object which we named questionInExample

And finally, we display the results of the test to make sure that our methods work and work as expected.

System.out.println("Question="+ questionInExample); // We will see: Question=What is Java?

Note, that in this explanation we provided another way of source-comments. One-liner comments start with this sequence: // and require no ending sequence as it ends with the end of line.
The comments line tells a compiler: "Do not pay attention to this line. This is just for people to clarify what is going on in the code."
Make sure that you press the ENTER key after the comments line, so the next line is visible to a compiler.


/**
* This class "Example" provides a sample of a standard Java class structure
* It starts with the header - the plan for the class.
* Then it provides class name; note that we create public classes.
* Then we introduce private data, in this case just one String variable, which we named question.
* Then we introduce public methods, which must manage data; usually they are set and get methods with the names reflecting the data names
* Then we provide the main() method; it must be public static void main(String[] args)
* In the main() method we usually create an object of the class and execute the methods of the class;
* The main() method usually serves us for testing the methods; we often display the results of the methods
*/
public class Example { // starting the class
// data
private String question;
// methods
public String getQuestion() {
return question;
}
public void setQuestion(String q) {
question = q;
}
// create main and test the methods
public static void main(String[] args) {
// create an object of the class
Example example = new Example();
// use setQuestion() method
example.setQuestion("What is Java");
// test getQuestion() method which returns the variable of the class question, which we just set to the value "What is Java?"
String result = example.getQuestion();
System.out.println(result); // display result
System.exit(0); // exit the main
} // end of main() method
} // end of class
Was it clear so far?

Please type the source into your Eclipse as recommended in the Assignments. In the future, it is highly recommended avoid copy/paste and type manually code in Eclipse.
This way more memory types are engaged helping you becoming an IT professional much sooner.

ClassCodeAndStructure

Sometimes programmers discuss their design by using pictures or graphs. Unified Modeling Language (UML) includes a set of graphical representations. To represent a class we use a rectangle with three parts: Class name, Data, and Methods. This image gives a good idea about your design without going into details of source implementation.

Assignments:

a) Open your Eclipse window and following the illustration below create a new package with the name day2
(Hints: Navigate to the left pane and find and open the week1 project; Right mouse click on the src - NEW - Package - day2. Done!)
b) Right-mouse click on the day2 package and create a new class Example
c) Look at the Example class in the illustration above and type the source of the Example class into Eclipse.
d) Right mouse click on the source - Run As - Java Application and you should see the results in the CONSOLE window
-- The result will be very modest - just one line, but this is exactly what program should produce!
e) Change something in the source and watch how Eclipse underscore with red the error
f) Fix the error and run the program again

g) Create another class named Person under the same package day2
h) Change the name of data variable from question to lastName
You will chang accordingly the names of the methods to setLastName and getLastName
i) Eclipse will underscore with red all related lines and you have to fix them according to new data lastName
j) Run As - Java Application
Note: Check if you made the change in the main() method. If you did not, the main method still runs the Person class.
Make this change: Person person = new Person();
No need to change the object name, compiler looks at the class name when creates this object with the keyword new. It is recommended to create object names in a meaningful way. Even a compiler does not care about the object names, this helps you and other programmers to better understand code. So, it is a good idea to change the name questionInExample to lastNameInExample, just to improve code reading.

k) Create another class named Dog with the data: dogName and dogBrand; You will have 4 methods now.
Do not forget headers and comments like in the Example class.

m) Create another class named Car with the data: model and productionYear.

Optional: Youtube video on classes https://www.youtube.com/watch?v=VUJFQpLL36Y and https://www.youtube.com/watch?v=XqTg2buXS5o&index=14&list=PLFE2CE09D83EE3E28

More links recommended by students:
https://www.tutorialspoint.com/java/java_object_classes.htm
// from this link i get some information about different types of variables , also there are lots of examples , as for me it`s very convinient.
https://www.youtube.com/watch?v=tnJzctk-Utk
// this video i found very interesting and helpful as well because there was explained Java Class Structure step by step.

Note that not all style recommendations are preserved in the videos and examples.

Extra points: Search Google and Youtube for the best presentations on this subject, read, watch, select and email 2 best links to dean@ituniversity.us

| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/>/**
<br/> * This class "Example" provides a sample of a standard Java class structure
<br/> * It starts with the header - the plan for the class.
<br/> * Then it provides class name; note that we create public classes.
<br/> * Then we introduce <b>private</b> data, in this case just one String variable, which we named <b>question</b>.
<br/> * Then we introduce <b>public</b> methods, which must manage data; usually they are set and get methods with the names reflecting the data names
<br/> * Then we provide the main() method; it must be <b>public static void main(String[] args)</b>
<br/> * In the main() method we usually create an object of the class and execute the methods of the class;
<br/> * The main() method usually serves us for testing the methods; we often display the results of the methods
<br/> */
<br/>public class Example { // starting the class
<br/>  // data
<br/>  private String question;
<br/>  // methods
<br/>  public String getQuestion() {
<br/>     return question;
<br/>  }
<br/>  public void setQuestion(String q) {
<br/>     question = q;
<br/>  }
<br/>  // create main and test the methods
<br/>  public static void main(String[] args) {
<br/>     // create an object of the class
<br/>     Example example = new Example();
<br/>     // use setQuestion() method
<br/>     example.setQuestion("What is Java");
<br/>     // test getQuestion() method which returns the variable of the class <b>question</b>, which we just set to the value "What is Java?"
<br/>    String result = example.getQuestion();
<br/>    System.out.println(result); // display result
<br/>    System.exit(0); // exit the main
<br/>  } // end of main() method
<br/>} // end of class
<br/>






Was it clear so far?


Please type the source into your Eclipse as recommended in the Assignments. In the future, it is highly recommended avoid copy/paste and type manually code in Eclipse.
This way more memory types are engaged helping you becoming an IT professional much sooner.

ClassCodeAndStructure

Sometimes programmers discuss their design by using pictures or graphs. Unified Modeling Language (UML) includes a set of graphical representations. To represent a class we use a rectangle with three parts: Class name, Data, and Methods. This image gives a good idea about your design without going into details of source implementation.

Assignments:

a) Open your Eclipse window and following the illustration below create a new package with the name day2
(Hints: Navigate to the left pane and find and open the week1 project; Right mouse click on the src - NEW - Package - day2. Done!)
b) Right-mouse click on the day2 package and create a new class Example
c) Look at the Example class in the illustration above and type the source of the Example class into Eclipse.
d) Right mouse click on the source - Run As - Java Application and you should see the results in the CONSOLE window
-- The result will be very modest - just one line, but this is exactly what program should produce!
e) Change something in the source and watch how Eclipse underscore with red the error
f) Fix the error and run the program again

g) Create another class named Person under the same package day2
h) Change the name of data variable from question to lastName
You will chang accordingly the names of the methods to setLastName and getLastName
i) Eclipse will underscore with red all related lines and you have to fix them according to new data lastName
j) Run As - Java Application
Note: Check if you made the change in the main() method. If you did not, the main method still runs the Person class.
Make this change: Person person = new Person();
No need to change the object name, compiler looks at the class name when creates this object with the keyword new. It is recommended to create object names in a meaningful way. Even a compiler does not care about the object names, this helps you and other programmers to better understand code. So, it is a good idea to change the name questionInExample to lastNameInExample, just to improve code reading.

k) Create another class named Dog with the data: dogName and dogBrand; You will have 4 methods now.
Do not forget headers and comments like in the Example class.

m) Create another class named Car with the data: model and productionYear.

Optional: Youtube video on classes https://www.youtube.com/watch?v=VUJFQpLL36Y and https://www.youtube.com/watch?v=XqTg2buXS5o&index=14&list=PLFE2CE09D83EE3E28

More links recommended by students:
https://www.tutorialspoint.com/java/java_object_classes.htm
// from this link i get some information about different types of variables , also there are lots of examples , as for me it`s very convinient.
https://www.youtube.com/watch?v=tnJzctk-Utk
// this video i found very interesting and helpful as well because there was explained Java Class Structure step by step.

Note that not all style recommendations are preserved in the videos and examples.

Extra points: Search Google and Youtube for the best presentations on this subject, read, watch, select and email 2 best links to dean@ituniversity.us



Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy