Website for the College study materials and cheats you can use

Translate

Discussion 4: Objects and Classes, Object-oriented-programming (OOP)

Discussion 4: Objects and Classes

According to Eck (2019), A class is slightly complicated to define exactly.  Some of the languages in regards to how classes 'describe' objects, or how objects 'belong to' classes is unclear.  It is better to say that classes are the 'blueprints' (p.202) for constructing objects.  They hold all of the objects and describe what variables and methods the objects may contain, and therefore can also be thought of as a kind of 'container'.  It is also a 'type' similar to the types of 'int' and 'bool', therefore it can also be used to specify types of variables.

An object, instead, is the particular 'unit' of focus in Java programming because it is Object-oriented-programming (OOP). They are independent little programs that can be executed in a different order from other objects or pieces of programs. OOP is a particular perception of programming that helps to conceptualize how to solve problems with computational tasks.  Rather than thinking about a script that solves a problem linearly from start to finish, objects allow programmers to create entities with particular 'behaviors' (Ibid, p.202) that hold information and interact with each other in the particular ways they want.  In this approach to programming, code is turned into particular tools and instruments that can act and interact together.   This float around in the computer's memory in a portion of memory called heaps (Ibid, p.205)

Since the language can sometimes be inadequate and in need of a metaphor, here is an example to help illustrate a small code I did to try and mimic our discussion here:
public class Discussion4 {

String studentcomment;    //these are attributes that belong to the class
int rate;
int ratings;
double avrating;
double numberofratings;     

public  void averageratings() {   //this is a method that belongs to the class.
avrating = ratings/numberofratings; }

public static void main(String[] args) {    //the main
Discussion4 AlexLloyd = new Discussion4();       //This is the creation of a new objects
Discussion4 OtherStudent = new Discussion4();
Discussion4 NewStudent = new Discussion4();

//UPEOPLE DISCUSSION PROCESS:

AlexLloyd.studentcomment = "Here is a comment!";    //imagine I make a comment

OtherStudent.rate = 1;      //the two students rate my comment
NewStudent.rate = 9;

AlexLloyd.ratings += OtherStudent.rate;   //ratings get added to my object
AlexLloyd.numberofratings++;              //another instance to calculate mean
AlexLloyd.ratings += NewStudent.rate; 
AlexLloyd.numberofratings++;

AlexLloyd.averageratings();                     //method that calculates the mean
System.out.println("The mean rating for '" + AlexLloyd.studentcomment + "' is " + AlexLloyd.avrating);   //print it out
}
}
OUTPUT:

The mean rating for 'Here is a comment!' is 5.0
Not perfect but I had fun trying to do this one this time. I hope it illustrates how objects and classes work.


References:
Eck, D. J. (2019). Introduction to programming using Java, version 8.1.

0 comments:

Post a Comment

Popular Posts

Categories

Contact Form

Name

Email *

Message *