JAVA関連のことを調べてみた2021年09月30日

JAVA関連のことを調べてみた2021年09月30日

【Java】継承についての復習

#目的

* 継承について学んだことを例題に沿って復習する

#実際の手順と実例
###1.継承とは

意味としてはそのままでした。

> あるクラスを作っているときに、クラスの一部を変更して使いまわしたいときに利用できる実装

参照:[今すぐ分かる!Javaで書くクラスの継承【初心者向け】](https://techacademy.jp/magazine/9246)

継承する元のクラスを「スーパークラス(親クラス、基底クラス)」と呼び、継承する先のクラスを「サブクラス(子クラス、派生クラス)」と呼びます。

###2.継承で覚えるべきこと

####1.抽象クラス

抽象メソッドを1つ以上持つクラスのことです。抽象メソッドとは、処理を記述せずにメソッド名や引数、戻り値だけを定義したメソッドのこと
→ 各継承先で(サブクラスで)処理だけ変えて使える

**基本構文**

“`java
アクセス修飾子 abstract class クラス名 {
// 抽象メソッドの宣言
アクセス修飾子 abstract 戻り値の型 メソッド名(引数);
}
“`

####2.イ

元記事を表示

Eclipse+MavenでJavaFXの環境構築を行う

Eclipse+MavenでJavaFXを使い、Eclipseからの実行と、実行可能jarの作成ができるまでの手順を記載する。
この手順で環境構築を行った例は以下。
https://github.com/tsyki/javafx-example

# JavaFX用のEclipseプラグイン「e(fx)clipse」を入れる(任意)
マーケットプレイスからインストールする。
入れなくてもJavaFXのアプリの実行はできるが、入れておくとFXMLの編集がやりやすくなる。
例えば、FXMLで指定したIDに対応するプロパティがコントローラで定義されていない、といった場合に警告を出してくれる。
2021/9版のpleiadesにe(fx)clipse3.7を入れて動作することを確認済み
# 新規Mavenプロジェクトを作成
「シンプルなプロジェクトの作成」をONにして作成する
# pom.xmlに以下を追加
“`xml

org.openjfx
javafx-controls

元記事を表示

Javaのまとめ

#はじめに
今日からJavaの学習も始めたので少しずつJavaとはどういったところが得意なのかこちらの記事でまとめてノウハウを蓄積していきたいと思います。プログラミング学習始めた時はJavaとJavascriptってややこしいなと思っていました、懐かしいですね

#Java得意なこと
Webアプリケーション、Androidアプリの開発。

#Java特徴
* コンパイラ言語
* オブジェクト指向言語

__コンパイラ言語__
コンパイラ言語とは、機械語に一括し変換してから実行するプログラミング言語。
処理が高速

__オブジェクト指向言語__
Java はオブジェクト指向。オブジェクト指向では何か特定の役割を果たすモノ(=オブジェクト)を作成し、そのオブジェクトに対して実行する処理を指示する。

余談として同じオブジェクト指向言語には「C++」「C#」「Kotlin」などがある。

__OSを選ばない__
Javaは、Windows,MacOS,Linuxなど、どのプラットフォームでも動く。
なぜなら、JVM上で動くから。

JVM = Java Virtual Machine(Jav

元記事を表示

[Java] JPAでbulk insertをする方法

## 概要
JPAでbulk insertを行いたいのだが、`@GeneratedValue`を使ってidを自動採番させるとbulk insertができない。`@GeneratedValue`を使わない場合、primary keyを明示的に入力しなければならないので面倒。

自動採番した上でbulk insertする方法はないのか。
中々情報がないが、JPAの仕様を理解し直すところも含め、なんとか実現方法がないのか調査してみた。

今回はPostgreSQLを使っているが、MySQLでも原因と解決策はほとんど同じである。

↓↓解決策だけ知りたい方は↓↓
**[解決策](#解決策)**

## 環境
– OpenJDK 15.0.4
– Spring Boot 2.5.4
– org.springframework.data:spring-data-jpa:2.5.4
– PostgreSQL 9.6.23

## bulk insertを有効にするための設定
まずはbulk insertを有効にするために、application.ymlで以下の設定をする。
これらの設定をし `rep

元記事を表示

[100%] CommonPrimeDivisors (codility lessons)

_Lesson 12_
## [Euclidean algorithm](https://app.codility.com/programmers/lessons/12-euclidean_algorithm/)

***
[Open reading material (PDF)](https://codility.com/media/train/10-Gcd.pdf)

> _Medium_
> ### [CommonPrimeDivisors](https://app.codility.com/programmers/lessons/12-euclidean_algorithm/common_prime_divisors/)
> Check whether two numbers have the same prime divisors.

***

#### Task description
A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few

元記事を表示

EclipseでMySQLを使う

#概要
MySQLで作成したデータベースの中身をEclipse(Java)で使えるようにする
詰まったことがあったので備忘録

#バージョン等
Windows10

Eclipse : 2021-06 (4.20.0)
Java : Java11
MySQL : 5.7.33

# 目次

1. [Javaプロジェクト作成](#Chapter1)
1. [プロジェクトにMySQLライブラリの追加](#Chapter2)
1. [MySQLからDBの値を取り出す](#Chapter3)
1. [エラー対処](#Chapter4)



# 1.Javaプロジェクト作成
DBtestという名前でプロジェクトを作成する。
今回はモジュールファイルも使用するので作成する。
**Eclipse2021の場合は下画像のように「モジュール->module-info.javaファイルの作成」に✓を入れないとファイルが作成されないので注意**
![image.png](https:

元記事を表示

[100%] ChocolatesByNumbers (codility lessons)

_Lesson 12_
## [Euclidean algorithm](https://app.codility.com/programmers/lessons/12-euclidean_algorithm/)

***
[Open reading material (PDF)](https://codility.com/media/train/10-Gcd.pdf)

> _Easy_
> ### [ChocolatesByNumbers](https://app.codility.com/programmers/lessons/12-euclidean_algorithm/chocolates_by_numbers/)
> There are N chocolates in a circle. Count the number of chocolates you will eat.

***

#### Task description

Two positive integers N and M are given. Integer N represents the numbe

元記事を表示

【Java】カプセル化の理解ついての復習をしていく

#目的

* カプセル化に関しての復習と定着

#実際の手順と実例
###1.カプセル化とは

* カプセル化とはオブジェクトの情報をカプセルのようにまとめ、中身を隠蔽したままオブジェクトを利用すること
* カプセル化のイメージは
* 外から防御されている
* カプセルに包まれている
* 「オブジェクト内のデータ」を包み、「オブジェクト外からの不正アクセス」を防ぐ

これらのイメージを持って問題を例にして考えていきます。

###2.例題

####問題

「Practice.java」を実行した結果が同じになるように、「NoCapsule.java」を「Capsule.java」に変更して、カプセル化したプログラムに変更してください。

**実行結果**

““`java
好きな食べ物はラーメンです。
週に1回食べています。
最近は週に2回食べるようになりました。。。
““`

““`NoCapsule.java
package practice;

public class NoCapsule {
public String food;

元記事を表示

【Minecraft Development】 Spigotプラグイン開発 最初の設定チートシート

# はじめに

このQiitaは **IntelliJ IDEA のプラグイン “Minecraft Development”** を使っている人向けの記事です。

VScodeなどで開発している人向けではないこと、それから自分向けの備忘録でもあります。

しっかりとした説明は省きます。

## まずやること

* IntelliJ IDEAに Minecraft Development を導入する
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/666481/8370d6a8-cf26-c9b8-12f5-91190408ecfb.png)
* JDKを導入する
* Minecraft v1.17以前のプロジェクトを開発するときは **JDK v1.8**
* Minecraft v1.17以降のプロジェクトを開発するときは **JDK v16**
* それぞれ変えないと開発できないので注意
* [JDKのダウンロードはこちらから](https://w

元記事を表示

Java Word文書からPDFに変換

Spire.Doc for Javaを利用することで、Wordファイルから、ただ数秒で様々なファイルに変換できます。これこそが、Javaでプログラミングを行う開発者にとって、とても使いやすいライブラリじゃないでしょうか。さあ、今回は、Word文書からPDFに変換する方法を紹介していきましょう!簡単なので、もしお役に立てば嬉しいと思います。

Spire.Doc for Javaとは?

Spire. Doc for Javaは、E-iceblue社が開発され、開発者が JavaプラットホームでWordのドキュメントを迅速か

[100%] CountSemiprimes (codility lessons)

_Lesson11_
## [Sieve of Eratosthenes](https://app.codility.com/programmers/lessons/11-sieve_of_eratosthenes/)

[Open reading material (PDF)](https://codility.com/media/train/9-Sieve.pdf)

> _Medium_
> ### [CountSemiprimes](https://app.codility.com/programmers/lessons/11-sieve_of_eratosthenes/count_semiprimes/)
> Count the semiprime numbers in the given range [a..b]

#### Task description

A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integer

[100%] CountNonDivisible (codility lessons)

_Lesson11_
## [Sieve of Eratosthenes](https://app.codility.com/programmers/lessons/11-sieve_of_eratosthenes/)

[Open reading material (PDF)](https://codility.com/media/train/9-Sieve.pdf)

> _Medium_
> ### [CountNonDivisible](https://app.codility.com/programmers/lessons/11-sieve_of_eratosthenes/count_non_divisible/)
> Calculate the number of elements of an array that are not divisors of each element.

#### Task description

You are given an array A consisting of N integers.

For each number A[i]

[100%] Peaks (codility lessons)

_Lesson10_
## [Prime and composite numbers](https://app.codility.com/programmers/lessons/10-prime_and_composite_numbers/)

[Open reading material (PDF)](https://codility.com/media/train/8-PrimeNumbers.pdf)

> _Medium_
> ### [Peaks](https://app.codility.com/programmers/lessons/10-prime_and_composite_numbers/peaks/)
> Divide an array into the maximum number of same-sized blocks, each of which should contain an index P such that A[P – 1] < A[P] > A[P + 1].

### Task description

A non-empty array A

[100%] Flags (codility lessons)

_Lesson10_
## [Prime and composite numbers](https://app.codility.com/programmers/lessons/10-prime_and_composite_numbers/)

[Open reading material (PDF)](https://codility.com/media/train/8-PrimeNumbers.pdf)

> _Medium_
> ### [Flags](https://app.codility.com/programmers/lessons/10-prime_and_composite_numbers/flags/)
> Find the maximum number of flags that can be set on mountain peaks.

#### Task description

A non-empty array A consisting of N integers is given.

A peak is an array element which i

[100%] MinPerimeterRectangle (codility lessons)

_Lesson10_
## [Prime and composite numbers](https://app.codility.com/programmers/lessons/10-prime_and_composite_numbers/)

[Open reading material (PDF)](https://codility.com/media/train/8-PrimeNumbers.pdf)

> _Easy_
> ### [MinPerimeterRectangle](https://app.codility.com/programmers/lessons/10-prime_and_composite_numbers/min_perimeter_rectangle/)
> Find the minimal perimeter of any rectangle whose area equals N.

#### Task description

An integer N is given, representing the area of some rectang

[100%] CountFactors (codility lessons)

_Lesson10_
## [Prime and composite numbers](https://app.codility.com/programmers/lessons/10-prime_and_composite_numbers/)

[Open reading material (PDF)](https://codility.com/media/train/8-PrimeNumbers.pdf)

> _Easy_
> ### [CountFactors](https://app.codility.com/programmers/lessons/10-prime_and_composite_numbers/count_factors/)
> Count factors of given number n.

#### Task description

A positive integer D is a factor of a positive integer N if there exists an integer M such that N = D * M.

For

[100%] MaxDoubleSliceSum (codility lessons)

_Lesson9_
## [Maximum slice problem](https://app.codility.com/programmers/lessons/9-maximum_slice_problem/)

[Open reading material (PDF)](https://codility.com/media/train/7-MaxSlice.pdf)

> _Medium_
> ### [MaxDoubleSliceSum](https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_double_slice_sum/)
> Find the maximal sum of any double slice.

#### Task description

A non-empty array A consisting of N integers is given.

A triplet (X, Y, Z), such that 0 ≤ X < Y < Z < N, is calle

[100%] MaxSliceSum (codility lessons)

_Lesson9_
## [Maximum slice problem](https://app.codility.com/programmers/lessons/9-maximum_slice_problem/)

[Open reading material (PDF)](https://codility.com/media/train/7-MaxSlice.pdf)

> _Easy_
> ### [MaxSliceSum](https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_slice_sum/)
> Find a maximum sum of a compact subsequence of array elements.

#### Task description

A non-empty array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, i

[100%] MaxProfit (codility lessons)

_Lesson9_
## [Maximum slice problem](https://app.codility.com/programmers/lessons/9-maximum_slice_problem/)

[Open reading material (PDF)](https://codility.com/media/train/7-MaxSlice.pdf)

> _Easy_
> ### [MaxProfit](https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_profit/)
> Given a log of stock prices compute the maximum possible earning.

#### Task description

An array A consisting of N integers is given. It contains daily prices of a stock share for a period of N cons

[100%] EquiLeader (codility lessons)

_Lesson8_
## [Leader](https://app.codility.com/programmers/lessons/8-leader/)

[Open reading material (PDF)](https://codility.com/media/train/6-Leader.pdf)

> _Easy_
> ### [EquiLeader](https://app.codility.com/programmers/lessons/8-leader/equi_leader/)
> Find the index S such that the leaders of the sequences A[0], A[1], …, A[S] and A[S + 1], A[S + 2], …, A[N – 1] are the same.

#### Task description

A non-empty array A consisting of N integers is given.

The leader of this array is the val