Java basic- Test 60 phút - tổng hợp kiến thức java căn bản
1. Tạo 1 lớp giao diện IInfor chứa hàm showInfor
2. Tạo lớp đối tượng People gồm các thuộc tính tên, tuổi, địa chỉ kế thừa từ lớp IInfor
- Tạo hàm input
Tạo lớp Car gồm các thuộc tính tên và màu -> kế thừa từ lớp IInfor
- Tạo hàm input
Viết code cho các lớp trên.
3. Trọng phương thức main của lớp Test tạo 2 đối tượng People và Car.
- tạo phương thức như sau.
public static void showInfor(ArrayList<IInfor> a) -> hàm này hiển thị thông tin tất cả đối tượng.
Viết chương trình và sử dụng hàm showInfor trong phương thức main.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![cuonglee [C1907L]](https://www.gravatar.com/avatar/b8c48ea2d04ab5a37bda2c50310d8a5c.jpg?s=80&d=mm&r=g)
cuonglee
2020-04-25 04:55:52
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.coban;
/**
*
* @author Admin
*/
public interface IInfor {
public void ShowInfor();
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.coban;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class People implements IInfor{
String ten;
int tuoi;
public People() {
}
public String getTen() {
return ten;
}
public void setTen(String ten) {
this.ten = ten;
}
public int getTuoi() {
return tuoi;
}
public void setTuoi(int tuoi) {
this.tuoi = tuoi;
}
public void input(){
System.out.println("nhap du lieu: ");
Scanner add = new Scanner(System.in);
System.out.println("nhap ten: ");
ten = add.nextLine();
System.out.println("nhap tuoi: ");
tuoi = add.nextInt();
}
@Override
public void ShowInfor() {
System.out.println("thong tin:");
System.out.println("ten: " + ten);
System.out.println("tuoi: "+ tuoi);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.coban;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Car implements IInfor{
String color,tenXe;
public Car() {
}
public String getTenXe() {
return tenXe;
}
public void setTenXe(String tenXe) {
this.tenXe = tenXe;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void input(){
System.out.println("nhap thong tin xe :");
Scanner input = new Scanner(System.in);
System.out.println("nhap ten xe: ");
tenXe = input.nextLine();
System.out.println(" nhap mau xe: ");
color = input.nextLine();
}
@Override
public String toString() {
return "Car{" + "color=" + color + ", tenXe=" + tenXe + '}';
}
@Override
public void ShowInfor() {
System.out.println( toString());
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.coban;
import java.util.ArrayList;
/**
*
* @author Admin
*/
public class Test {
public static void main(String[] args) {
ArrayList<IInfor> list = new ArrayList<>();
People people = new People();
Car car = new Car();
people.input();
car.input();
list.add(people);
list.add(car);
ShowInfor(list);
}
public static void ShowInfor(ArrayList<IInfor>a){
for(IInfor show :a){
show.ShowInfor();
}
}
}
![Vũ Việt Đức [C1907L]](https://www.gravatar.com/avatar/114894070fbd15fc0c29ffdeab37f4b5.jpg?s=80&d=mm&r=g)
Vũ Việt Đức
2020-03-23 13:55:17
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lession5;
/**
*
* @author ADMIN
*/
public interface IInfor {
public void showInfor();
}
![Vũ Việt Đức [C1907L]](https://www.gravatar.com/avatar/114894070fbd15fc0c29ffdeab37f4b5.jpg?s=80&d=mm&r=g)
Vũ Việt Đức
2020-03-23 13:54:52
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lession5;
import java.util.Scanner;
/**
*
* @author ADMIN
*/
public class Car implements IInfor{
String name, color;
public Car() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public void showInfor() {
System.out.println(toString());
}
@Override
public String toString() {
return "Car{" + "name=" + name + ", color=" + color + '}';
}
public void input(){
Scanner input = new Scanner(System.in);
System.out.print("Tên: ");
name = input.nextLine();
System.out.print("Màu: ");
color = input.nextLine();
}
}
![Vũ Việt Đức [C1907L]](https://www.gravatar.com/avatar/114894070fbd15fc0c29ffdeab37f4b5.jpg?s=80&d=mm&r=g)
Vũ Việt Đức
2020-03-23 13:54:25
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lession5;
import java.util.Scanner;
/**
*
* @author ADMIN
*/
public class People implements IInfor{
String name, address;
int age;
public People() {
}
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 void setAge(int age) {
this.age = age;
}
@Override
public void showInfor() {
System.out.println(toString());
}
@Override
public String toString() {
return "People{" + "name=" + name + ", address=" + address + ", age=" + age + '}';
}
public void input(){
Scanner input = new Scanner(System.in);
System.out.print("Tên: ");
name = input.nextLine();
System.out.print("Tuổi: ");
age = Integer.parseInt(input.nextLine());
System.out.print("Địa chỉ: ");
address = input.nextLine();
}
}
![Vũ Việt Đức [C1907L]](https://www.gravatar.com/avatar/114894070fbd15fc0c29ffdeab37f4b5.jpg?s=80&d=mm&r=g)
Vũ Việt Đức
2020-03-23 13:54:01
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lession5;
import java.util.ArrayList;
/**
*
* @author ADMIN
*/
public class Test {
public static void main(String[] args){
ArrayList<IInfor> infors = new ArrayList<>();
People p1 = new People();
People p2 = new People();
People p3 = new People();
Car c1 = new Car();
Car c2 = new Car();
p1.input();
p2.input();
p3.input();
c1.input();
c2.input();
infors.add(p1);
infors.add(p1);
infors.add(p1);
infors.add(c1);
infors.add(c2);
showInfor(infors);
}
public static void showInfor(ArrayList<IInfor> a){
for(IInfor item: a){
item.showInfor();
}
}
}
![Hoàng Quang Huy [C1907L]](https://www.gravatar.com/avatar/76e646e11f1674fba03bddd0ae020813.jpg?s=80&d=mm&r=g)
Hoàng Quang Huy
2020-03-22 10:27:45
package test;
import java.util.Scanner;
public class Infor {
String name;
public Infor() {
}
public Infor(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void showInfor(){
System.out.println("Tên: "+ name);
}
public void input(){
Scanner input = new Scanner(System.in);
System.out.println("Nhập tên: ");
name = input.nextLine();
}
}
---------------------------------------------------------------------------
package test;
import java.util.Scanner;
public class People extends Infor {
String address;
int age;
public People(){}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public People(String address, int age, String name) {
super(name);
this.address = address;
this.age = age;
}
@Override
public void input() {
Scanner input = new Scanner(System.in);
super.input();
System.out.println("Nhập tuổi: ");
age = Integer.parseInt(input.nextLine());
System.out.println("Nhập địa chỉ: ");
address = input.nextLine();
}
@Override
public void showInfor() {
super.showInfor();
System.out.println("Tuổi: " + age);
System.out.println("Địa chỉ: " + address);
}
}
-------------------------------------------------------------------------
package test;
import java.util.Scanner;
public class Car extends Infor{
String color;
public Car(String color, String name) {
super(name);
this.color = color;
}
public Car() {}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public void input(){
Scanner input = new Scanner(System.in);
System.out.println("Nhập tên xe: ");
name = input.nextLine();
System.out.println("Nhập màu: ");
color = input.nextLine();
}
@Override
public void showInfor() {
System.out.println("Tên xe: "+name);
System.out.println("Màu: " + color);
}
}
---------------------------------------------------------------------------
package test;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
People people;
Car car;
ArrayList <Infor> list = new ArrayList<>();
Scanner input = new Scanner(System.in);
System.out.println("Nhập số lượng người: ");
int peoplenum = Integer.parseInt(input.nextLine());
System.out.println("Nhập số lượng xe: ");
int carnum = Integer.parseInt(input.nextLine());
for (int i = 0; i < peoplenum; i++) {
people = new People();
System.out.println("Nhập thông tin người thứ "+ (i+1));
people.input();
list.add(people);
}
for (int i = 0; i < carnum; i++) {
car = new Car();
System.out.println("Nhập thông tin xe thứ "+ (i+1));
car.input();
list.add(car);
}
showInfor(list);
}
public static void showInfor(ArrayList<Infor> a){
System.out.println("Danh sách thông tin: ");
for (int i = 0; i < a.size(); i++) {
a.get(i).showInfor();
System.out.println("----------------");
}
}
}
![nguyễn văn huy [T1907A]](https://www.gravatar.com/avatar/b107d14d7d43c142b68c12c377262371.jpg?s=80&d=mm&r=g)
nguyễn văn huy
2020-03-22 04:27:21
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Tonghop;
/**
*
* @author ASUS
*/
public interface IInfor {
public void showInfor();
}
////////
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Tonghop;
import java.util.*;
/**
*
* @author ASUS
*/
public class people implements IInfor{
String name,adress;
int age;
public people() {
}
public people(String name, String adress, int age) {
this.name = name;
this.adress = adress;
this.age = age;
}
public void input(){
Scanner scan=new Scanner(System.in);
System.out.println("nhap ten:");
name=scan.nextLine();
System.out.println("nhap tuoi:");
age=Integer.parseInt(scan.nextLine());
System.out.println("nhap dia chi");
adress=scan.nextLine();
}
public void showInfor(){
System.out.println(toString());
}
@Override
public String toString() {
return "people{" + "name=" + name + ", adress=" + adress + ", age=" + age + '}';
}
}
/////
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Tonghop;
import java.util.*;
/**
*
* @author ASUS
*/
public class car implements IInfor {
String name, mau;
public car() {
}
public car(String name, String mau) {
this.name = name;
this.mau = mau;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("nhap ten:");
name = scan.nextLine();
System.out.println("nhap mau:");
mau = scan.nextLine();
}
public void showInfor() {
System.out.println(toString());
}
@Override
public String toString() {
return "car{" + "name=" + name + ", mau=" + mau + '}';
}
}
/////
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Tonghop;
import java.util.*;
/**
*
* @author ASUS
*/
public class test {
public static void main(String[] args) {
ArrayList<IInfor>a=new ArrayList<>();
people pe=new people();
pe.input();
a.add(pe);
car ca=new car();
ca.input();
a.add(ca);
}
public static void showInfor(ArrayList<IInfor> a){
for (int i = 0; i < a.size(); i++) {
a.get(i).showInfor();
}
}
}
![Nguyễn Hữu Đạt [C1907L]](https://www.gravatar.com/avatar/343beaebafd55688b65478947cb718be.jpg?s=80&d=mm&r=g)
Nguyễn Hữu Đạt
2020-03-20 14:04:10
package test;
/**
*
* @author Laptop88
*/
public class IInfor {
void input();
void showInfor();
}
-------------------------------------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.util.Scanner;
/**
*
* @author Laptop88
*/
public class People extends IInfor {
String name;
String address;
int age;
public People() {
}
public void input(){
Scanner input = new Scanner(System.in);
System.out.println("Name:= ");
this.name = input.nextLine();
System.out.println("Addres:= ");
this.address = input.nextLine();
System.out.println("Age:= ");
this.age = Integer.parseInt(input.nextLine());
}
public void showInfor() {
System.out.println(toString());
}
public String toString() {
return "People{" + "name=" + name + ", address=" + address + ", age=" + age + '}';
}
}
-------------------------------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.util.Scanner;
/**
*
* @author Laptop88
*/
public class Car extends IInfor{
String color;
String name;
public Car() {
}
public void input(){
Scanner input = new Scanner(System.in);
System.out.println("Name:= ");
this.name = input.nextLine();
System.out.println("Color:= ");
this.color = input.nextLine();
}
public void showInfor() {
System.out.println(toString());
}
public String toString() {
return "Car{" + "name=" + name + ", color=" + color + '}';
}
}
------------------------------------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.util.ArrayList;
/**
*
* @author Laptop88
*/
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here'
IInfor peopleObject = new People();
IInfor carObject = new Car();
peopleObject.input();
carObject.input();
peopleObject.input();
ArrayList<IInfor> peopleList = new ArrayList<>();
ArrayList<IInfor> carList = new ArrayList<>();
peopleList.add(peopleObject);
carList.add(carObject);
showInfor(peopleList);
showInfor(carList);
}
public static void showInfor(ArrayList<IInfor> a){
for (IInfor x : a){
x.showInfor();
}
}
}
![hoangkhiem [C1907L]](https://www.gravatar.com/avatar/d3627ce786997fab24d1b790c91c6368.jpg?s=80&d=mm&r=g)
hoangkhiem
2020-03-20 13:45:57
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demi;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class giaodien {
void showInfor() {
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demi;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class People extends giaodien{
private String ten,diachi;
private int tuoi;
public People() {
}
public People(String ten, String diachi, int tuoi) {
this.ten = ten;
this.diachi = diachi;
this.tuoi = tuoi;
}
public String getTen() {
return ten;
}
public void setTen(String ten) {
this.ten = ten;
}
public String getDiachi() {
return diachi;
}
public void setDiachi(String diachi) {
this.diachi = diachi;
}
public int getTuoi() {
return tuoi;
}
public void setTuoi(int tuoi) {
this.tuoi = tuoi;
}
public void input() {
Scanner Nhap = new Scanner(System.in);
System.out.println(" Tên; ");
ten = Nhap.nextLine();
System.out.println(" Tuổi");
tuoi = Integer.parseInt(Nhap.nextLine());
System.out.println(" địa chỉ");
diachi = Nhap.nextLine();
}
@Override
public void showInfor() {
System.out.println(toString());
}
@Override
public String toString() {
return "People{" + "tên là =" + ten + ", Địa Chỉ là =" + diachi + ", Tuổi là=" + tuoi + '}';
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demi;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Car extends giaodien {
private String ten, mau;
public Car() {
}
public Car(String ten, String mau) {
this.ten = ten;
this.mau = mau;
}
public String getTen() {
return ten;
}
public void setTen(String ten) {
this.ten = ten;
}
public String getMau() {
return mau;
}
public void setMau(String mau) {
this.mau = mau;
}
@Override
public void showInfor() {
System.out.println(toString());
}
@Override
public String toString() {
return "Car{" + "ten=" + ten + ", mau=" + mau + '}';
}
public void input() {
Scanner Nhap = new Scanner(System.in);
System.out.println("Nhap ten: ");
ten = Nhap.nextLine();
System.out.println("Nhap mau: ");
mau = Nhap.nextLine();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demi;
import java.util.ArrayList;
/**
*
* @author Admin
*/
public class Test {
public static void main(String[] args) {
ArrayList<giaodien> list = new ArrayList<>();
People people = new People();
people.input();
Car car = new Car();
car.input();
list.add(people);
list.add(car);
showInfor(list);
}
public static void showInfor(ArrayList<giaodien> a){
for (int i = 0; i < a.size(); i++) {
a.get(i).showInfor();
}
}
}
![Lê Trí Dũng [C1907L]](https://www.gravatar.com/avatar/720bc9428f32c3816dde6806f7d743c8.jpg?s=80&d=mm&r=g)
Lê Trí Dũng
2020-03-20 13:44:38
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
/**
*
* @author Dzung
*/
public class IInfor {
void showInfor();
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.util.Scanner;
/**
*
* @author Dzung
*/
public class People extends IInfor {
public class People {
private String name, address;
private int age;
public People(String name, String address, int age) {
this.name = name;
this.address = address;
this.age = age;
}
public void input(){
Scanner scan = new Scanner(System.in);
System.out.println("Insert name: ");
name = scan.nextLine();
System.out.println("Insert age: ");
age = scan.nextInt();
System.out.println("Insert address: ");
address = scan.nextLine();
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" + "name=" + name + ", address=" + address + ", age =" + age }';.
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.util.Scanner;
/**
*
* @author Dzung
*/
public class Car extends IInfor {
public class Car {
private string name,color;
public Car(string name, string color) {
this.name = name;
this.color = color;
}
public void input(){
Scanner scan = new Scanner(System.in);
System.out.println("Insert name: ");
name = scan.nextLine();
System.out.println("Insert color: ");
color = scan.nextLine();
}
public string getName() {
return name;
}
public void setName(string name) {
this.name = name;
}
public string getColor() {
return color;
}
public void setColor(string color) {
this.color = color;
}
@Override
public String toString() {
return "Car{" + "name=" + name + ", color=" + color + '}';
}
}
}