練習問題
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>
|
||||
19
Question11-ConstructorAndStatic/src/main/java/Atm.java
Normal file
19
Question11-ConstructorAndStatic/src/main/java/Atm.java
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
public class Atm {
|
||||
int credit;
|
||||
|
||||
|
||||
public Atm(){
|
||||
this.credit = 100000;
|
||||
}
|
||||
public void withdraw(Wallet wallet,int money) {
|
||||
if(this.credit < money) {
|
||||
System.out.println("預金残高不足です。現金の引き出しに失敗しました。");
|
||||
}
|
||||
else {
|
||||
credit = credit - money;
|
||||
wallet.money = wallet.money + money;
|
||||
System.out.println("ATMの残り残高は" + credit + "です");
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Question11-ConstructorAndStatic/src/main/java/Cat.java
Normal file
14
Question11-ConstructorAndStatic/src/main/java/Cat.java
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
public class Cat {
|
||||
String name;
|
||||
|
||||
public Cat(String name){
|
||||
this.name = name;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public Cat() {
|
||||
this.name = "ミケ";
|
||||
}
|
||||
}
|
||||
27
Question11-ConstructorAndStatic/src/main/java/Q11_1.java
Normal file
27
Question11-ConstructorAndStatic/src/main/java/Q11_1.java
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
public class Q11_1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
Wallet wallet = new Wallet();
|
||||
|
||||
wallet.money = 10000;
|
||||
|
||||
User user = new User();
|
||||
|
||||
user.name = "太郎";
|
||||
|
||||
|
||||
user.wallet = wallet;
|
||||
|
||||
user.confirmWallet();
|
||||
|
||||
user.useMoney(1000);
|
||||
|
||||
user.confirmWallet();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
22
Question11-ConstructorAndStatic/src/main/java/Q11_2.java
Normal file
22
Question11-ConstructorAndStatic/src/main/java/Q11_2.java
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
public class Q11_2 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
Wallet wallet = new Wallet();
|
||||
|
||||
wallet.money = 10000;
|
||||
|
||||
Atm atm = new Atm();
|
||||
|
||||
atm.withdraw(wallet, 100000);
|
||||
|
||||
System.out.println("ATMの残り残高" + wallet.money + "円です");
|
||||
|
||||
atm.withdraw(wallet, 100000);
|
||||
|
||||
System.out.println("財布の中の残額は" + wallet.money + "円です");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
13
Question11-ConstructorAndStatic/src/main/java/Q11_3.java
Normal file
13
Question11-ConstructorAndStatic/src/main/java/Q11_3.java
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
public class Q11_3 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Cat cat1 = new Cat("タマ");
|
||||
System.out.println(cat1.name);
|
||||
|
||||
Cat cat2 = new Cat();
|
||||
System.out.println(cat2.name);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
18
Question11-ConstructorAndStatic/src/main/java/Q11_4.java
Normal file
18
Question11-ConstructorAndStatic/src/main/java/Q11_4.java
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
public class Q11_4 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
|
||||
Student student = new Student();
|
||||
student.name = "太郎";
|
||||
Student.group = "A";
|
||||
Student.teacher = "山田";
|
||||
|
||||
student.studentProfile();
|
||||
|
||||
Student.groupProfile();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
14
Question11-ConstructorAndStatic/src/main/java/Student.java
Normal file
14
Question11-ConstructorAndStatic/src/main/java/Student.java
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
public class Student {
|
||||
String name;
|
||||
static String group;
|
||||
static String teacher;
|
||||
|
||||
public void studentProfile() {
|
||||
System.out.println(name + "は" + group + "教室の生徒です。");
|
||||
}
|
||||
|
||||
public static void groupProfile() {
|
||||
System.out.println(group + "教室の担任は" + teacher + "先生です。");
|
||||
}
|
||||
}
|
||||
17
Question11-ConstructorAndStatic/src/main/java/User.java
Normal file
17
Question11-ConstructorAndStatic/src/main/java/User.java
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
public class User {
|
||||
String name;
|
||||
Wallet wallet;
|
||||
|
||||
public void useMoney(int money) {
|
||||
|
||||
wallet.money = wallet.money - money;
|
||||
|
||||
System.out.println(name + "は財布の中のお金を" + money + "円使用しました");
|
||||
}
|
||||
|
||||
public void confirmWallet() {
|
||||
System.out.println(name + "の財布の中の残額は" + wallet.money + "円です");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
public class Wallet {
|
||||
int money;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user