What you should know
Object oriented programming is not going to be easy the first time you discover it. Probably the easiest way to break this down is to imagine an actual physical object. An example would be a vehicle. Imagine you are required to build a car. Just think about the parts you will need, such as an engine, framework, wheels, transmission, etc. The vehicle is the actual object. Now the parts of the vehicle are called the attributes. These attributes can also be used to build other engines (or objects). There can also be more one than one type of object. For example you could build two cars, each related to their own manufacturer. To understand this even more, I have an example below that first breaks this up in a simple way. As you read further down you will also see some visual examples of how this works. However if you examine the picture of the mustang you should be able to see it "visually".
Object Example: Vehicle (object) engine, framework, wheels, transmission (attributes) Vehicle 1 ("Mustang") = new (engine, framework, wheels, transmission); Vehicle 2 ("Camaro") = new (engine, framework, wheels, transmission);
Now see the parts: Vehicle1 mustang = new Vehicle (5.0, "Custom style", 4, "TR-3650"); Vehicle2 camaro = new Vehicle (2.8, "factory", 4, "V6"); Understanding attributesLook very closely at the attributes for Vehicle 1 (the mustang). You can probably tell by reading the parameters that it is a 5.0 liter with a custom style framework. It has 4 tires and a TR-3650 transmission type. These attributes can be anything you desire. For example I could have done something like this instead, which is a completely different object. Person male = new Person ("Alexander", 180, "blue", 26); If you saw this: A man named "Alexander with a weight of 180lbs, blue eyes, and age of 26, then congratulations you are starting to understand how object oriented languages work. The new is a keyword that is used to represent a new object. Remember what you get learned about the attributes. Each attribute can be of a different type. Now look back at the Vehicle example. The first attribute shows 5.0. Since this value contains decimals, it is known as a double. Also consider that this value can also be a number without decimals, such as a 5 for example. When you use a value without decimals it is known as an int which stands for an Integer. The thing to know is that you can use any decimal range or any integer range. More attribute types
Now look at the next attribute in sequence. Here is the object again. Vehicle1 mustang = new Vehicle (5.0, "Custom style", 4, "TR-3650"); The next attribute "Custom style" is a different type known as a String. The important thing to know is that anytime you see two quotes "" with text embedded inside you can use any characters in this area, including numbers. The numbers will not be integers or doubles since the compiler is looking at them as a character only. A series of characters is called a String. Here's another example of a String. Person male = new Person ("Alexander"); When the compiler examines anything inside the strings it will store that in memory. In this example the compiler will see the first attribute as a String "Alexander". You will learn later how a String is initialized in an inheritance program. Object oriented programming is famous for inheritance which basically borrows other attributes from other methods and classes. Methods will also be covered. For now just understand the different between these keywords, int, double, String. Integers reviewedEarlier in this lesson you learned about the int keyword. Here are a series of integer values. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 Integers can be used to calculate values with. The same goes for double. For example look at this problem below. 5 * 5 = 25 Now see the problem as a computer statement. int math = 5 * 5; The = sign works the same as it would on a math problem. It assigns the values to the right of the equals sign to the calculations listed. Try this program for example. It demonstrates what was just discussed and produces a simple output of a math problem. Later in these tutorials you will learn how to do more advanced map, including inheriting methods to allow more complex math to be possible. Click the Next button to advance to the next page to see the results of the output.
Math program output
Once you enter the program in Eclipse and click the run arrow
Now we will examine the program code again to highlight a few new things.
The newest thing we have observed is this line: System.out.println(5 * 5); The compiler using println to skip a line on the display. What you should pay closer attention to however if this section (5 * 5); Here the compiler is taking two integers 5 and 5 to be multiplied together. If you recall from the main page, I spoke about understanding operators. The operator here is the * (asterisk). Also note you can do this: System.out.println(10 - 5); Instead of multiplying the integers this time, we are subtracting them. Here are all the operators again: + - * / (addition, subtraction, multiplication, division) You can use math on any of them. Recapping what you learned
In this lesson we covered how to build objects, how attributes work, and how to calculate simple problems in Java. In coming lessons, we will be discussing more advanced concepts with objects, attributes, and variables. Continue to practice what you have learned before proceeding to the next section. Looking aheadIn future tutorials you will learn how to do advanced programming with objects. I will show you step by step how to use inheritance with methods and classes. This will take some time to devour, so as always it is important to make sure you know the basics of programming first. If necessary review any topics that may not still be clear to you. In order to gain a more firm grip on the Java programming language later make sure you understand the basics, such as input, output, variables and what was covered in this lesson. Until next timeI have enjoyed teaching you the concepts you have learned in this lesson. Don't burn yourself out. Instead take some time off to do something you enjoy until we meet again. One of the most important things any student can do is to get plenty of sleep before learning something new.
|
Tip on this page: If you are confused about a topic be sure to check out the links on the left as they go into more detail about breaking down a specific concept. |