[Studying Java Core] #2

Inwook Baek ·2021년 9월 28일
0

Java Study

목록 보기
2/6
post-thumbnail
post-custom-banner

Computer Programming

It is a process of writing a set of instructions (also known as code) that a machine can understand and making the machine follow them.

So, it’s not really about choosing a language. It’s more about choosing a field. If you want to create Android apps, choose Java; for iOS apps choose Swift, and if you want to develop games, learn C, etc. And if your answer to the question "why" is "to make a lot of money, idk", consider the most popular programming languages and start there.

Introduction to OOP

Fundamentals

Object-oriented programming (OOP) is a programming paradigm based on the concept of objects that interact with each other to perform the program functions. Each object can be characterized by a state and behavior. An object’s current state is represented by its fields, and an object’s behavior is represented by its methods.

Basic princicples of OOP

Encapsulation ensures bundling (=encapsulating) of data and the methods operating on that data into a single unit. It also refers to the ability of an object to hide the internal structure of its properties and methods.
Data abstraction means that objects should provide the simplified, abstract version of their implementations. The details of their internal work usually aren't necessary for the user, so there's no need to represent them. Abstraction also means that only the most relevant features of the object will be presented.
Inheritance is a mechanism for defining parent-child relationships between classes. Often objects are very similar, so inheritance allows programmers to reuse common logic and at the same time introduce unique concepts into the classes.
Polymorphism literally means "having many forms" and is a concept related to inheritance. It allows programmers to define different implementations for the same method. Thus, the name (or interface) remains the same, but the actions performed may differ. For example, imagine a website that posts three main types of text: news, announcements, and articles. They are somewhat similar in that they all have a headline, some text, and a date. In other ways, they are different: articles have authors, news bulletins have sources, and announcements have a date after which they become irrelevant. It is convenient to write an abstract class with general information for all publications to avoid copying it every time and store what is different in the appropriate derived classes.

  1. An object-oriented program consists of a set of interacting objects.
  2. According to the principle of encapsulation, the internal implementation of the object is not accessible to the user.
  3. An object may have characteristics: fields and methods.
  4. An object is an instance of a class (type);
  5. A class is a more abstract concept than an individual object; it may be considered a template or blueprint that describes the common structure of a set of similar objects.

Boolean Type

The boolean is a data type that has only two possible values: false and true. This is also known as the logical type.

Logical operators

NOT is a unary operator that reverses the boolean value. It is denoted as !.

boolean f = false; // f is false
boolean t = !f;    // t is true

AND is a binary operator that returns true if both operands are true, otherwise, it is false. It is denoted as &&.

boolean b1 = false && false; // false
boolean b2 = false && true;  // false
boolean b3 = true && false;  // false
boolean b4 = true && true;   // true 

OR is a binary operator that returns true if at least one operand is true, otherwise, it returns false. It is denoted as ||.

boolean b1 = false || false; // false
boolean b2 = false || true;  // true
boolean b3 = true || false;  // true
boolean b4 = true || true;   // true

XOR (exclusive OR) is a binary operator that returns true if boolean operands have different values, otherwise, it is false.

boolean b1 = false ^ false; // false
boolean b2 = false ^ true;  // true
boolean b3 = true ^ false;  // true
boolean b4 = true ^ true;   // false

Below are the logical operations sorted in order of decreasing their priorities in expressions: ! (NOT), ^ (XOR), && (AND), || (OR).

An interesting thing is that the && and || operators don't evaluate the second argument if it isn't necessary. When the first argument of the && operator evaluates to false, the overall value must be false; and when the first argument of the || operator evaluates to true, the overall value must be true.

This behavior is known as short-circuit evaluation (do not confuse it with an electrical short circuit). It reduces the computation time, but can also be used to avoid some errors in programs. We will discuss this in the following topics.

Relational Operators

Example 1:
Write a program that reads the numbers a, b, c and checks if there's a pair of them that adds up to exactly 20.

The program must output true or false.

Sample Input:
4 16 7

Sample Output:
true

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // put your code here
        int q = scanner.nextInt();
        int w = scanner.nextInt();
        int e = scanner.nextInt();

        boolean res = q + w == 20 || q + e == 20 || w + e == 20;

        System.out.println(res);
    }
}

Example 2:
Groundhogs like to throw fun parties, and at their parties, they like to eat Reese's peanut butter cups. But not too many of them, or they feel sick! A successful groundhog party will have between 10 and 20 Reese's peanut butter cups, inclusive, unless it is the weekend, in which case they will need 15 to 25 Reese's peanut butter cups, inclusive.

Write a Java program that reads two values:

the first is the number of Reese's peanut butter cups;
the second is a boolean representing whether it is the weekend.
The program must print a boolean value that indicates whether the party was successful.

The program must output true or false.

Sample Input:
16 false

Sample Output:
true

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // put your code here
        int cupsNum = scanner.nextInt();
        boolean isWeekend = scanner.nextBoolean();
        
        boolean condition1 = cupsNum >= 10 && cupsNum <= 20 && !isWeekend;
        boolean condition2 = cupsNum >= 15 && cupsNum <= 25 && isWeekend;
        boolean partySucc = condition1 || condition2;
        
        System.out.println(partySucc);
    }
}
post-custom-banner

0개의 댓글