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.

Definition: Formal parameter, The actual parameter is much more straight forward

Definition:
1. Formal parameter:

The formal parameter appears at the definition of the method/subroutine. The formal parameter is a placeholder for the actual input of the method/subroutine when it's called. This is necessary since the compiler needs to know what kind of input is needed for the function when the function is called. Without the placeholder, the input format is unknown and the method or subroutine might not function as intended when called.

This can also be explained from the concept of the "black box". While we might not need to know the actual content of the box, we do need to be sure about what kind of input and output does the box needs/generate. Without knowing the precise format to communicate with the box, we won't be able to properly interact with the box and utilize the already developed function/method/subroutine.

2. Actual parameter:

The actual parameter is much more straight forward. It is the content that was passed to the called function. It might be different each time. However, it's format should be consistent with the formal parameter. The actual parameter provides the flexibility to the method/subroutine so that we don't need to develop separate methods for a similar purpose with slightly different details.

Example:
import javax.swing.JOptionPane;

public class Discussion_3 {
static void print(String message_formal) {
JOptionPane.showMessageDialog(null, message_formal);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String message_actual = "Hello, UoPEOPLE!";
print(message_actual);

}

}

In this example, the formal parameter is the message_formal in the method defining line. In the line, the message_formal tells Java that the print function requires a String input as a parameter. When the function is called by the main function, the function took message_actual as an actual parameter. This time, the actual parameter became the input that was given to the called "print" function. Accordingly, the message is displayed in the panel.

What is the difference between these two parameters? Discussing the concept of parameters, What are the parameters for?

Discussing the concept of parameters
A parameter is defined as a mechanism used for passing information into a subroutine(Eck, D. J. (2019) p 152). Since a subroutine is like a black box in which a user doesn’t need to know what is inside the subroutine before using it(Eck, D. J. (2019) p 141). And passing in a parameter is the only way to customize the behavior of a subroutine so as to adapt it to a particular situation (Eck, D. J. (2019) p 152).

 What are the parameters?

 A parameter is used to define the specification of a subroutine (Eck, D. J. (2019) p 152). Elaborately, it is used to pass in information to a subroutine (Eck, D. J. (2019) p 152).

They are two different types of Parameters in java programming.

 These parameters are Formal parameters and Actual parameters which is also called an argument.

 What is the difference between these two parameters?

 Formal parameters Are parameters that are used when defining a subroutine (Eck, D. J. (2019) p 152). It is important to know that are not values but they are much like a variable which means that these parameters must have a type when it is being defined. (Eck, D. J. (2019) p 152). Actual parameters: Are parameters that are used when calling a subroutine (Eck, D. J. (2019) p 153). These parameters are values and must respect the type of the formal parameter.

 Example of Actual and Formal parameters


In the code above the formal parameter is String name with String being the type of the parameter which is defined in the subroutine SayNmae.

and the actual parameter is the string "junior" that was passed in when we were calling the SayName subroutine.

We need to know that a subroutine when being defined can also have no formal parameter in this case when we call the subroutine no actual parameter will be passed in.


Defining and explaining Syntax of a programming language, Differences between syntax errors and semantics errors

Defining and explaining the Syntax of a programming language

A program in computer science is a sequence of instructions that a computer can execute (Eck, D.J. 2019 pg 34). This sequence of instructions needs to be written in programming languages (formal languages established by humans) before the computer can understand them and make execution. The reason is that human language (natural language) really differs from computer language in the sense that computer language is very strict, unambiguous (Eck, D. J. 2019 pg 34), they are not literal meaning that they mean exactly what they say: a correct input gives correct output but an error input gives an error output ( Downey.A. 2015 pg27) whereas human language is the opposite of the computer language characteristics. So with all these being said, the syntax of a language are the rules that determine how a program should be constructed in that specific language since every formal language has its own syntax.


Defining and explaining Semantics of a programming language

Semantics are referred to as the meaning of a written program. Elaborately a programming language semantics is the set of rules that determine the meaning of a program in a specific language ( Eck, D.J. 2019 pg 35).


Differences between syntax errors and semantics errors.

First, let us define what these two errors mean when they occur in programming in so doing we can easily grab the differences between them.


A syntax error is an error in a program that makes it impossible for the interpreter to interpret (Downey 2015 pg 37). This usually occurs when we do not respect the syntax of the programming language we are utilizing. And since formal languages are not literal, meaning that they mean exactly they say, the input we put is the output we get (Downey.A. 2015 pg 27).


A semantic error is an error in a program that makes the program do the opposite of what the programmer intended the program to do (Downey.A. 2015 pg 37). or causes the program to run incorrectly (Eck. Dj 2019 pg 74). Some semantic errors occur when a variable is not properly initialized. (when another variable type is used to initialize another variable type).


With each definition, we can clearly see the difference between these two types of programming errors. In a nutshell, a Syntax error has to do with the syntactic error of the programming language we are using and Semantic error has to do with the error occurred in the meaning of what the programmer wants the program to produce.


Example of Syntax error:

public class syntaxandsemanticerror {

public static void main(String[] args) {

/* This program will produce a syntax error because i missed putting the variable

* "principle" near the if conditional in parenthesis */

int principle;

principle = 87;

Double interest;


if principle > 98

interest = principle * 0.02;

else

interest = principle *2.1;

System.out.println(interest);


Example of Semantic errors:

// Semantic error

/* This will output a semantic error because i used a type variable for another variable type on number2*/

int number;

int calculate;

int number2;

number = 43;

number2 = 32.32;

calculate = number + number2;



// Second semantic error


/* I want (the programmer) the program to print in a new line but it will do something different

* This is because i used "print" instead of "println*/

String name;

name = "Israel";

System.out.print("Welcome, " + name + " Pleased to meet you");


// Note that this program won't be able to run due to the syntax and semantic errors it contains to run it try removing the errors

}

}


Please find below an attached file of the program.


REFERENCES:

Eck, D. J. (2019).Introduction to programming using Java, version 8.1. retrieved from http://math.hws.edu/javanotes.

Downey, A.(2015). Think python, How to think like a computer science

DISCUSSION UNIT 1 Introduction to Programming

PROGRAMMING 1

                                                                                                                            CHICK ETIEN NDEH

                                                                                                                   University of the People

DISCUSSION UNIT 1

Introduction to Programming



Briefly explain what is meant by the syntax and the semantics of a programming language. Give an example to illustrate the difference between a syntax error and a semantics error.



Any syntax of a programing language shows its grammar, and the semantics is its meaning. A program with a syntax error cannot be compiled reason been that during the code an error courses the program not to give any output.

while a program with a semantic error can be compiled and run but gives an incorrect result. In this case, the code will run smoothly but will give a different meaning rather than the output expected.

According to (Arnab S IITian, JavaHolic, Raconteur 2017 ) Syntax errors occur during the parsing of input code and are caused by grammatically incorrect statements. Typical errors might be an illegal character in the input, a missing operator, two operators in a row, two statements on the same line with no intervening semicolon, unbalanced parentheses, a misplaced reserved word, etc.


Semantic errors occur during the execution of the code after it has been parsed as grammatically correct. These have to do not with how statements are constructed, but with what they mean. Such things as incorrect variable types or sizes, nonexistent variables, subscripts out of range, and the like, are semantic errors.


 A missing semicolon in a program is an example of a syntax error because the compiler will find the error and report it. If N is an integer variable, then the statement "frac = 1/N;" is probably an error of semantics. The value of 1/N will be 0 for any N greater than 1. It's likely that the programmer meant to say 1.0/N.



Splitting a string over two lines: In most cases, Java doesn’t care if your code appears on one or more lines. However, if you split a string across lines so that the string contains a newline character, then the compiler will object.

The answer is to end the string on the first line with a double quote, add a plus sign to tell the compiler you want to concatenate (add) this string with another string, and then continue the string on the next line like this (John P. M  Java eLearning Kit For Dummies April 2014.p01, para 2)


      System.out.print("This is a really long " +
            "string that appears on two lines.");
Semantic errors

Use of a non-initialized variable:

int i;

i++; // the variable i is not initialized

 Errors in expressions:

String s = "...";

int a = 5 - s; // the - operator does not support arguments of type String


References

John P. M  Java eLearning Kit For Dummies April 2014.p01,para 2 retrieved from https://www.dummies.com/programming/java/syntactical-errors-in-java/
Arnab S IITian, JavaHolic, Raconteur 2017 retrieved from https://www.quora.com/What-are-the-differences-between-syntax-errors-and-semantic-errors

Universal Declaration of Human Rights is a document that was written for all nations

Universal Declaration of Human Rights
Unit 3
University of the People


Universal Declaration of Human Rights is a document that was written for all nations, a kind of guideline for everyone regardless of age, nationality, race, gender. This document highlights freedom and equality for all. It encourages all nations to follow this declaration. (Levin Institute, (n.d.) 2019). Over time these rights have improved the rights of many, one example of this is now over 198 counties allow women to vote (Declaration of Human, (n.d.)2018).

First of all, I feel that all the articles are important. I feel that life experience could make you look at the articles in different ways. So, at this time these are important to me. The 3 articles that I chose are Article 2, Article 5, Article 11 (Human Rights (n.d.), 1989). Article 2 Freedom of Discrimination. I justify this by my own experiences. I have been discriminated against in several different ways. It has changed my life in ways I can’t even explain. These experiences changed my life and it had a lasting impact on me. Next, I chose Article 5 Freedom from Torture and Degrading Treatment. I justify this because torture is ramped around the world, in some places, it’s getting worse. In an article in USA Today (2017) it brought out that sexual abuse of women has worsened, along with extrajudicial killing increased in the Philippines. There’s a similar problem in China with unlawful deprivation of life, executions, and torture. So, this is the reason I feel Article 5 is important. Article 21 Right to Participate in Government and in Free Elections. In America we have the freedom of choice, we can vote to let our opinion be heard. It’s important to be able to have a part in changing policy, in turn, it’s a start to changing the world.

The case study I’m going to talk about is The Case Against Human Rights (Posner E. 2014) There was a bricklayer who lived in Rio De Janeiro. Police suspected him of drug trafficking. He was arrested then disappeared. No one ever saw him again. This caused outrage with the citizens. There was an investigation that leads to the arrest of 10 police officers who were charged with torturing and murdering this poor man. You would think that because Brazil is
one of the largest democracies in the world, they wouldn’t have this problem. More than 1000 killings happened by police in Rio De Janeiro in one year. They say most likely all deaths to were executions. These types of atrocities are happing all over the world, South Africa, Dominican, Republic, and Iran to name a few (Posner E. 2014). So, I feel that this supports the Articles I have chosen discrimination, freedom from torture, and voting because this is still happening, and we have a voice to help stop it.

This is why the declaration of human rights is so important. No matter which Article we chose every single one of them is important. We all see the world in a different way, and I feel that’s why many articles cover a little of all the issues around the world.




Reference

Levin Institute, (n.d.), (2019) Human Rights
https://my.uopeople.edu/pluginfile.php/770765/mod_book/chapter/229104/glob101humanrights.pdf


How has the declaration of human rights changed, n.d.(2018) Peace news.,
https://www.peacenews.com/single-post/2018/08/11/How-has-the-Declaration-of-Human-
Rights-changed-the-world

Human Right Here and Now (n.d.) (1998)
http://hrlibrary.umn.edu/edumat/hreduseries/hereandnow/Part-5/8_udhr-abbr.htm


USA Today, (2017) U.S. Corruption, torture, discrimination worse in parts of the world. Retrieved from https://www.usatoday.com/story/news/world/2017/03/03/united-states-free-expression-decline-worldwide/98706280/

Posner E. (2014) Case against human rights Retrieved by https://www.google.com/amp/s/amp.theguardian.com/new/2014/dec/4/-sp-case-against-human-rights

Popular Posts

Categories

Contact Form

Name

Email *

Message *