10/16自習
This commit is contained in:
6
Question11-ConstructorAndStatic/.classpath
Normal file
6
Question11-ConstructorAndStatic/.classpath
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
|
||||
<classpathentry kind="src" path="src/main/java"/>
|
||||
<classpathentry kind="output" path="build/classes"/>
|
||||
</classpath>
|
||||
1
Question11-ConstructorAndStatic/.gitignore
vendored
Normal file
1
Question11-ConstructorAndStatic/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build/
|
||||
23
Question11-ConstructorAndStatic/.project
Normal file
23
Question11-ConstructorAndStatic/.project
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question11-ConstructorAndStatic</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,9 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
|
||||
org.eclipse.jdt.core.compiler.compliance=21
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=21
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<faceted-project>
|
||||
<installed facet="java" version="21"/>
|
||||
</faceted-project>
|
||||
21
Question11-ConstructorAndStatic/src/main/java/Atm.java
Normal file
21
Question11-ConstructorAndStatic/src/main/java/Atm.java
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
public class Atm {
|
||||
//フィールドの宣言
|
||||
int credit = 100000;
|
||||
|
||||
|
||||
//withdrawメソッドの生成
|
||||
public void withdraw(Wallet2 wallet,int money) {
|
||||
|
||||
wallet.money = wallet.money + money;
|
||||
|
||||
if(credit < money) {
|
||||
System.out.println("預金残高不足です。現金の引き出しに失敗しました。");
|
||||
} else {
|
||||
System.out.println("ATMの残り残高はx" + (credit - money) + "です");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
|
||||
public class Question11_01 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
//Walletのインスタンス生成
|
||||
Wallet wallet = new Wallet();
|
||||
|
||||
//wallet の money フィールドに 10000 を代入
|
||||
wallet.money = 10000;
|
||||
|
||||
|
||||
//Userのインスタンス生成
|
||||
User user = new User();
|
||||
|
||||
//user の name フィールドに 太郎 を代入
|
||||
user.name = "太郎";
|
||||
|
||||
user.wallet = wallet;
|
||||
|
||||
//user の confirmWallet メソッドを実行
|
||||
user.confirmWallet();
|
||||
|
||||
//user の useMoney メソッドを、引数 1000 で実行
|
||||
user.useMoney(1000);
|
||||
|
||||
//user の confirmWallet メソッドを再度実行
|
||||
user.confirmWallet();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
|
||||
public class Question11_02 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
//Wallet 型の変数 wallet を宣言し、Wallet のインスタンスを代入
|
||||
Wallet2 wallet = new Wallet2();
|
||||
|
||||
//wallet の money フィールドに 10000 を代入
|
||||
wallet.money = 10000;
|
||||
|
||||
|
||||
//Atm 型の変数 atm を宣言し、Atm のインスタンスを代入
|
||||
Atm atm = new Atm();
|
||||
|
||||
//atm の withdraw メソッドを、第一引数に wallet、第二引数に 100000 を渡して呼び出す
|
||||
atm.withdraw(wallet,100000);
|
||||
|
||||
//wallet の money フィールドの値を出力
|
||||
System.out.println(wallet.money);
|
||||
|
||||
atm.withdraw(wallet,100000);
|
||||
|
||||
System.out.println(wallet.money);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
public class Question11_03 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
22
Question11-ConstructorAndStatic/src/main/java/User.java
Normal file
22
Question11-ConstructorAndStatic/src/main/java/User.java
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
public class User {
|
||||
//フィールドの宣言
|
||||
String name;
|
||||
Wallet wallet;
|
||||
|
||||
|
||||
//Wallet.javaのmoneyの値が欲しい
|
||||
|
||||
|
||||
//お金を使うメソッドの生成(useMoney)
|
||||
public void useMoney(int money) {
|
||||
//財布の中のお金-使ったお金
|
||||
wallet.money = wallet.money - money;
|
||||
System.out.println( name + "は財布の中のお金を" + money + "円使用しました");
|
||||
}
|
||||
|
||||
//財布の残高確認メソッドの生成(confirmWallet)
|
||||
public void confirmWallet() {
|
||||
System.out.println( name + "の財布の中の残額は" + wallet.money +"円です");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
public class Wallet {
|
||||
//フィールドの宣言
|
||||
int money;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
public class Wallet2 {
|
||||
//フィールドの宣言
|
||||
int money;
|
||||
}
|
||||
Reference in New Issue
Block a user