[Processing x Java] Data type and object-oriented programming

This article is for understanding the structure of the program through Processing. In How to use variables, I wrote about data types and variables. This time I will write about data types and object-oriented programming. Understanding data types will help you understand classes and object-oriented programming.

table of contents 0. Data type

  1. Object-oriented programming

0. Data type

0-0. ** What is data ...? **

Before writing about data types, we talk about "what is data". The data is information. Weight, height, eye color, car number, daylight hours, precipitation, address, images, etc. .. In programming terms, variables are data, functions are data, and classes are data.

** Point **: Data is represented in computer memory as a set of ** bits (0 and 1) **. ** Point : Data has a type ( data type **) and is saved by type.

0-1. ** About data types **

There are two types of data (data types): ** basic data type ** and ** composite data type **.

** [Basic data type] **

The base data type stores ** "only one data element" **.

name Data element size(bit) Range of values
boolean Boolean value 1 true or false
byte Part-Time Job 8 -128 to 127
char 1 alphanumeric character, symbol 16 0 to 65535
int integer 32 -2,147,483,648 to 2,147,483,647
float Floating point number 32 3.40282347E +From 38-3.40282347E + 38
color color 32 16,777,216 colors

** [Composite data type] **

Composite data types contain ** "multiple data and functions (methods)" **.

name Data element Method
String String length(),charAt(),equals(),indexOf(),toLowerCase(),toUpperCase()...
PImage image loadPixels(),updatePixels(),resize(),get(),set(),mask(),filter(),blend(),save()...
PFont font list()
PShape Shape beginShape(),endShape(),translate(),rotate(),scale()...
PVector Coordinate set(),mag(),magsq(),add(),sub(),mult(),dist(),normalize(),lerp()...
:

0-2. ** Example of using data type **

[Basic data type]

Data type: boolean

◯ A program that changes the color of lines using boolean

boolean01.java


//Variable declaration and initialization
boolean b = false;

size(640, 360);
background(0);
//Build-in variable width is size()It can be used only after it is defined in.
int middle = width/2;

//Loop to draw a line
for (int i = 0; i <= width; i += 20) {
  //Decide when it is true and when it is false.
  if (i < middle) {
    b = true;
  } else {
    b = false;
  }
  //If true
  if (b == true) {
    stroke(255,0,0);
  }
  //If false
  if (b == false) {
    stroke(0,0,255);
  }
  //Draw a line
  line(i,0,i,height);
}

b.png

◯ A program that controls the movement of a circle by pressing the mouse

boolean02.java


//Define a float type variable x that indicates the x coordinate of the circle.
float x = 0;
//Define a boolean type variable going that determines whether or not the circle advances.
boolean going = false;

void setup(){
  size(500,500);
}
//infinite loop(Make an animation)
void draw(){
  background(0);
  fill(255);
  //Coordinates of the center of the circle= (x,height/2)
  ellipse(x,height/2,30,30);
  
  //If going=If True
  if(going){
    //Add x by 2
    x += 2;
  }
}

//Runs only once when the mouse is pressed
void mousePressed(){
  //Substitute the truth value opposite to the current truth value for going.
  //If going is True, substitute False.
  //If going is False, substitute True.
  going = !going;
  //Outputs to the console when the mouse is pressed.
  println("mouse is pressed!!");
}

ezgif.com-gif-maker (2).gif

Data type: char

◯ A program that efficiently outputs characters using a loop

char.java


//Create a char type variable letter and make initial settings.
char letter = 'A';

//char type is a character(Alphabet)Since it has the order of, use it.
for(int i = 0;i < 26;i++){
    print(letter);
    //Add letters one by one
    letter++;
}
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Data type: int

◯ A program that efficiently outputs multiples using a loop

int.java


//Declare a variable n of type int and initialize it.
int n = 3;

//Repeat the loop 5 times
for(int i = 0;i < 5;i++){
    println(n);
    // n = 2n
    n *= 2;
}
3
6
12
24
48

Data type: color

color.java


//color variable blue of type color()Define using.
color blue = color(0,0,255);
background(blue);

color.png

[Composite data type] ----------------------------------------------------------- --------------------

Data type: String

◯ A program that uses the methods of the String class

String.java


//Create an object s1 of class String and put its contents.
String s1 = "breakfast"
//Dot(.)Use to access the methods of the object.
println(s1.length());
9

s1 is a ** String class object **, which is a ** instance variable ** in terms of variable type.

Data type: PImage

◯ Program to display images

PImage.java


//Declaration of object
//Create an object called img by embodying the PImage class.
PImage img;

void setup(){
    size(100,100);
    //Put the contents in the object.
    img = loadImage("~.png ");
}

void draw(){
    //Use an object.
    image(img,0,0);
}

1. Object-oriented programming

1-0. ** First check the terms **

** Object-oriented programming ** A program that defines related functions and variables together.

class A group of related fields and methods. It is often compared to a recipe.

field A variable defined in the class.

** Method ** A function defined in a class.

object An instance of a class (a concrete example). It has the same field names and methods as the original class. It is often compared to a dish made using a recipe.

instance An object of a class.

** Instance variable ** A type of variable. A variable created when the object was created.

variable An element of data with a certain name. Every variable has a value, a data type, and a scope.

scope The range (block) in which a variable can be accessed in a program. The place where you can use it depends on where you define it.

1-1. ** Programming with classes **

** ◯ I want to use a class **

The class is just a template and cannot be used as is. So

** "Create an object by embodying a class and treat it like a variable while taking advantage of the characteristics of the object" **

I will do it like that.

** ◯ Materialize the class **

The class contains ** fields and methods **. I want to use this in a program. However, the class is ** just a template ** and is an abstract entity. So, let's make the class a concrete existence (object) and operate on it.

** ◯ How to materialize the class **

Let's look at a simple example. Here, the object s1 is created from the class String.

String.java


//Declaration of object
String s1;
//Object definition
s1 = "breakfast"
//Use of objects
println(s1.length());
9

Where s1 is ** Object ** (concrete entity made from class) And ** Instance variable ** (variable generated when an instance is created) is.

The name (appearance) differs depending on where you look from and where you set the standard, but the contents are the same. ** There are various names. ** **

** ◯ Treat the object like a variable while taking advantage of its characteristics **

Every object has the fields and methods that the class has.

· Field = Variable defined in the class

-Method = function defined in the class

Now that I've created the object, I think I can finally use the fields and methods of the class.

To access the fields and methods of an object, use dots (.). The object itself is treated like a variable.

String.java


//Object definition
String str1 = "goodjob"; 
//Access the methods of the object
str1 = str1.toUpperCase();
//Use an object
println(str1);
GOODJOB

To use it like a variable ** Definition (declaration-> initial setting)-> use ** It means to use it like this.

So in summary, str1 here is

□ Has fields and methods. (Characteristics of the object) □ Use dots to access your fields and methods. (How to use) And □ Has value, data type, and scope. (Variable characteristics) □ Use after defining (declaring, initial setting). (How to use)

These are some of the ways to program with classes.

Finally

Thank you for reading. We appreciate your opinions and suggestions in order to make the article even better.

Recommended Posts

[Processing x Java] Data type and object-oriented programming
Java programming (variables and data)
[Java] Data type ①-Basic type
Java variable declaration, initialization, data type (cast and promotion)
About Java basic data types and reference type memory
Java learning memo (data type)
Basic data type and reference type
AWS SDK for Java 1.11.x and 2.x
[Java] Loop processing and multiplication table
[Java] Data type / matrix product (AOJ ⑧ Matrix product)
Java basic data types and reference types
Summary of object-oriented programming using Java
[Java] Exception types and basic processing
[Introduction to Java] Variables and types (variable declaration, initialization, data type)
Java date data type conversion (Date, Calendar, String)
Data processing using stream API from Java 8
[Processing x Java] Construction of development environment
Object-oriented programming
Use PostgreSQL data type (jsonb) from Java
[Personal memo] Java data type is annoying
[Java] Data type / string class cheat sheet
Java study # 3 (type conversion and instruction execution)
Java programming (static clauses and "class variables")
[Java] Object-oriented
[Java] Calculation mechanism, operators and type conversion
Parallel and parallel processing in various languages (Java edition)
Organized memo in the head (Java --Data type)
Java type conversion
[Java] Enumeration type
java programming basics
Java Optional type
Object-oriented FizzBuzz (Java)
[Java] Object-oriented summary_Part 1
Object-oriented programming terminology
Java thread processing
Selenium x Java
[Java] Object-oriented syntax-Constructor
Object-oriented (Java) basics
Java double type
java Generic Programming
Java string processing
Java and JavaScript
XXE and Java
[Java] Object-oriented summary_Part 2
[Java] Multi-thread processing
[Java] Stream processing
[Java] Object-oriented syntax-Package
java iterative processing
[Android / Java] Screen transition and return processing in fragments
Vectorize and image MNIST handwritten digit image data in Java
[# 2 Java] What is object-oriented programming that you often hear?