缺了很久的作業
但還是沒寫完~~
第三張作業
public class Customer {
public String id="A123456789";
public String name="";
public String address ;
public char gender ='M';
public boolean married=false;
public int age;
public String email;
}
/*1. String型別的id, 初值為"A123456789"
2. String型別的name, 初值為""
3. String型別的address
4. char型別的gender, 初值為'M'
5. boolean型別的married, 初值為false
6. int型別的age
7. String型別的email
*/
實作方法 測試程式
public static void main(String[] args) {
// TODO code application logic here
Customer customer=new Customer();
customer.id="A123456789";
customer.name="張三";
System.out.println(customer);
System.out.println("customer id: "+ customer.id);
System.out.println("ctstomer name: "+customer.name);
System.out.println("ctstomer address:"+customer.address);
System.out.println("customer email:"+customer.email);
System.out.println("customer gender:"+customer.gender);
System.out.println("customer age:"+customer.age);
System.out.println("customer married: "+customer.married);
}
}
RunFile
init:
deps-jar:
Compiling 1 source file to C:\java\mod02\Homework\build\classes
compile-single:
run-single:
Customer@c17164
customer id: A123456789
ctstomer name: 張三
ctstomer address:null
customer email:null
customer gender:M
customer age:0
customer married: false
BUILD SUCCESSFUL (total time: 0 seconds)
-----------------------------------------------------------------------------
public class Product {
public int id;
public String name;
public double unitPrice;
public boolean free;
public int stock;
}
/*1. int型別的id
2. String型別的name
3. double型別的unitPrice
4. boolean型別的free
5. int型別的stock
*/
測試程式
public class TestProduct {
public static void main(String[] args) {
// TODO code application logic here
Product p =new Product();
System.out.println(p.name); // 印出null
p.id=1;
p.name="簡報鯊";
p.free=false;
p.unitPrice=2850;
p.stock=10;
System.out.println(p.name); //印出簡報鯊
}
}
/*(2) 在main方法中宣告型別為Product的變數p,並建立Product物件指派給變數p。
(3) 加上:System.out.println(p.name);
(4) 指派p的id為1、name為簡報鯊、unitPrice為2850、free為false、stock為10
(5) 再加上:System.out.println(p.name);
*/
RunFile
init:
deps-jar:
compile-single:
run-single:
null
簡報鯊
BUILD SUCCESSFUL (total time: 0 seconds)
第四章作業
public class Customer {
private String id="A123456789";
private String name="";
private String address ;
private char gender ='M';
private boolean married=false;
private int age;
private String email;
public String getId(){
return id;
}
public void setId(String id){
this.id=id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address=address;
}
public int getAge() {
return age;
}
public String getEmail() {
return email;
}
public char getGender() {
return gender;
}
public boolean isMarried() {
return married;
}
public void setAge(int age) {
this.age = age;
}
public void setEmail(String email) {
this.email = email;
}
public void setGender(char gender) {
this.gender = gender;
}
public void setMarried(boolean married) {
this.married = married;
}
}
public class TestCustomer {
public static void main(String[] args) {
// TODO code application logic here
Customer customer=new Customer();
customer.setId("A123456789");
customer.setName("張三");
System.out.println(customer);
System.out.println("customer id: "+ customer.getId());
System.out.println("ctstomer name: "+customer.getName());
System.out.println("ctstomer address:"+customer.getAddress());
System.out.println("customer email:"+customer.getEmail());
System.out.println("customer gender:"+customer.getGender());
System.out.println("customer age:"+customer.getAge());
System.out.println("customer married: "+customer.isMarried());
}
}
public class Product {
private int id;
private String name;
private double unitPrice;
private boolean free;
private int stock;
public int getId(){return id;}
public void setId(int id){this.id=id;}
public String getName(){return name;}
public void setName(String name){this.name=name;}
public double getUnitPrice(){return unitPrice;}
public void setUnitPrice(double unitPrice){this.unitPrice=unitPrice;}
public boolean isFree(){return free;}
public void setFree(boolean free){this.free=free;}
public int getStock(){return stock;}
public void setStock(int stock){this.stock=stock;}
public void incStock(int amount){
stock=stock+amount;
}
public void decStock(int amount){
stock=stock-amount;
}
}
public class ProductService {
public double calculateProductInventory(Product product){
return product.getUnitPrice()*product.getStock() ;
}
}
//測試程式
public class TestProduct {
public static void main(String[] args) {
// TODO code application logic here
Product p =new Product();
System.out.println(p.getName());
p.setId(1);
p.setName("簡報鯊");
p.setFree(false);
p.setUnitPrice(2850);
p.setStock(11);
System.out.println(p.getId());
System.out.println(p.getName());
System.out.println(p.getUnitPrice());
ProductService service=new ProductService();
System.out.println(service.calculateProductInventory(p));
}
}
Run File
null
1
簡報鯊
2850.0
31350.0
BUILD SUCCESSFUL (total time: 0 seconds)
留言列表