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)
![trung [C1907L]](https://www.gravatar.com/avatar/67c23432e4710f33dd14e580d41b0379.jpg?s=80&d=mm&r=g)
trung
2020-03-20 13:44:09
/*
* 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 iinfor;
/**
*
* @author prdox
*/
interface IInfor {
public void input();
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 iinfor;
import java.util.Scanner;
/**
*
* @author prdox
*/
public class People implements IInfor{
String name,address;
int age;
public People() {
}
@Override
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());
}
@Override
public void showInfor() {
System.out.println(toString());
}
@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 iinfor;
import java.util.Scanner;
/**
*
* @author prdox
*/
public class Car implements IInfor{
String name,color;
public Car() {
}
@Override
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();
}
@Override
public void showInfor() {
System.out.println(toString());
}
@Override
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 iinfor;
import java.util.ArrayList;
/**
*
* @author prdox
*/
public class Test {
public static void main(String[] args) {
IInfor peopleObject = new People();
IInfor carObject = new Car();
peopleObject.input();
carObject.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();
}
}
}
![Nguyễn Hoàng Anh [C1907L]](https://www.gravatar.com/avatar/5b7bb435cae0d0a0fd414f6fdd0adc87.jpg?s=80&d=mm&r=g)
Nguyễn Hoàng Anh
2020-03-20 13:41:33
/*
* 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 test60p;
/**
*
* @author Redmibook 14
*/
public class Car extends IInfo {
String color;
public void input(String name, String color) {
this.name = name;
this.color = color;
}
@Override
public void showInfo() {
super.showInfo();
System.out.println("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 test60p;
/**
*
* @author Redmibook 14
*/
public class IInfo {
String name;
public void showInfo(){
System.out.println("Name : " + name);
}
}
/*
* 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 test60p;
/**
*
* @author Redmibook 14
*/
public class People extends IInfo {
int age;
String diaChi;
public void input(String name, int age, String diaChi) {
this.name = name;
this.age = age;
this.diaChi = diaChi;
}
@Override
public void showInfo() {
super.showInfo();
System.out.println("Age : " + age);
System.out.println("diaChi : " + diaChi);
}
}
/*
* 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 test60p;
import java.util.*;
/**
*
* @author Redmibook 14
*/
public class Test {
public static void showInforCar(ArrayList<Car> arr) {
for (int i = 0; i < arr.size(); i++) {
System.out.println("Car" + (i + 1) + "info : ");
arr.get(i).showInfo();
}
}
public static void showInfor(ArrayList<People> arr) {
for (int i = 0; i < arr.size(); i++) {
System.out.println("People " + (i + 1) + "info : ");
arr.get(i).showInfo();
}
}
public static void messCase() {
System.out.println("1.Input person ");
System.out.println("2.Input car");
System.out.println("3.Show ");
System.out.println("4.Exit ");
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<People> people = new ArrayList();
ArrayList<Car> car = new ArrayList();
while (true) {
messCase();
int choice = Integer.parseInt(input.nextLine());
switch (choice) {
case 1:
System.out.println("Name : ");
String name = input.nextLine();
System.out.println("Age : ");
int age = Integer.parseInt(input.nextLine());
System.out.println("Address : ");
String address = input.nextLine();
People p = new People();
p.input(name, age, address);
people.add(p);
break;
case 2:
System.out.println("Name : ");
String cname = input.nextLine();
System.out.println("Color : ");
String color = input.nextLine();
Car c = new Car();
c.input(cname, color);
car.add(c);
break;
case 3:
showInforCar(car);
showInfor(people);
break;
case 4:
return;
default:
break;
}
}
}
}
![Ngô Quang Huy [C1907L]](https://www.gravatar.com/avatar/8e54dbf5994077ce599f49278164ae79.jpg?s=80&d=mm&r=g)
Ngô Quang Huy
2020-03-20 13:38:27
package kiemtra;
public class Infor {
public void showInfor(){
}
}
package kiemtra;
import java.util.Scanner;
public class People extends Infor{
String Name;
int Age;
String Address;
public People() {
}
public People(String Name, int Age, String Address) {
this.Name = Name;
this.Age = Age;
this.Address = Address;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public int getAge() {
return Age;
}
public void setAge(int Age) {
this.Age = Age;
}
public String getAddress() {
return Address;
}
public void setAddress(String Address) {
this.Address = Address;
}
public void input(){
Scanner in = new Scanner(System.in);
System.out.println("Nhap vao Ten nguoi: ");
Name= in.nextLine();
System.out.println("Nhap vao Tuoi: ");
Age= Integer.parseInt(in.nextLine());
System.out.println("Nhap vao Dia chi: ");
Address= in.nextLine();
}
@Override
public void showInfor() {
System.out.println("Ten la: "+Name);
System.out.println("Tuoi la: "+Age);
System.out.println("Dia chi la: "+Address);
}
}
package kiemtra;
import java.util.Scanner;
public class Car extends Infor{
String Name;
String Color;
public Car() {
}
public Car(String Name, String Color) {
this.Name = Name;
this.Color = Color;
}
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;
}
public void input(){
Scanner in = new Scanner(System.in);
System.out.println("Nhap vao Ten xe: ");
Name= in.nextLine();
System.out.println("Nhap vao Mau: ");
Color= in.nextLine();
}
@Override
public void showInfor() {
System.out.println("Ten la: "+Name);
System.out.println("Mau la: "+Color);
}
}
package kiemtra;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
People nguoi = new People();
Car oto = new Car();
ArrayList<Infor> List = new ArrayList<>();
nguoi.input();
List.add(nguoi);
oto.input();
List.add(oto);
showInfor(List);
}
public static void showInfor(ArrayList<Infor> a){
for(int i=0;i<a.size();i++){
a.get(i).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-20 13:34:34
/*
* 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 aptech;
import java.util.ArrayList;
/**
*
* @author DUC
*/
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<IInfor> 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<IInfor> a){
for(int i = 0; i < a.size(); i++){
a.get(i).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-20 13:34:14
/*
* 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 aptech;
import java.util.Scanner;
/**
*
* @author ADMIN
*/
public class Car extends IInfor{
String name, color;
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;
}
public Car() {
}
public Car(String name, String color) {
this.name = name;
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-20 13:33:58
/*
* 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 aptech;
import java.util.Scanner;
/**
*
* @author DUC
*/
public class People extends IInfor{
String name, address;
int age;
public People() {
}
public People(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
@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("Nhập tên: ");
name = input.nextLine();
System.out.print("Nhập tuổi: ");
age = Integer.parseInt(input.nextLine());
System.out.print("Nhập đị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-20 13:33:40
/*
* 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 aptech;
/**
*
* @author DUC
*/
public class IInfor {
public void showInfor(){
}
}
![Trần Mạnh Dũng [T1907A]](https://www.gravatar.com/avatar/ee4661d1f9133c241d6c8287c7ea8ceb.jpg?s=80&d=mm&r=g)
Trần Mạnh Dũng
2020-03-16 13:25:26
/*
* 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 assignment60phut;
import java.util.Scanner;
public class Car implements IInfor {
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 sc = new Scanner(System.in);
System.out.println("Nhap ten: ");
ten = sc.nextLine();
System.out.println("Nhap mau: ");
mau = sc.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 assignment60phut;
public interface 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 assignment60phut;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<IInfor> list = new ArrayList<>();
People pp = new People();
pp.input();
Car car = new Car();
car.input();
list.add(pp);
list.add(car);
showInfor(list);
}
public static void showInfor(ArrayList<IInfor> a) {
for (int i = 0; i < a.size(); i++) {
a.get(i).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 assignment60phut;
import java.util.Scanner;
public class People implements IInfor {
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 void input() {
Scanner sc = new Scanner(System.in);
System.out.println("Nhap ten; ");
ten = sc.nextLine();
System.out.println("Nhap tuoi");
tuoi = Integer.parseInt(sc.nextLine());
System.out.println("Nhap dia chi");
diachi = sc.nextLine();
}
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;
}
@Override
public void showInfor() {
System.out.println(toString());
}
@Override
public String toString() {
return "People{" + "ten=" + ten + ", diachi=" + diachi + ", tuoi=" + tuoi + '}';
}
}
![hoangduyminh [T1907A]](https://www.gravatar.com/avatar/33675cc9fc3762fd323389a179aa047f.jpg?s=80&d=mm&r=g)
hoangduyminh
2020-03-13 07:17:30
/*
* 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 Bt522;
import java.util.Scanner;
/**
*
* @author Minh
*/
public class People implements IInfor{
private String Name, Address;
private int age;
public People() {
}
public People(String Name, String Address, int age) {
this.Name = Name;
this.Address = Address;
this.age = age;
}
public void input() {
Scanner input = new Scanner(System.in);
System.out.println("Nhap ten :");
Name = input.nextLine();
System.out.println("Nhap dia chi:");
Address = input.nextLine();
System.out.println("Nhap tuoi:");
age = Integer.parseInt(input.nextLine());
}
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 + '}';
}
}
/*
* 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 Bt522;
import java.util.Scanner;
/**
*
* @author Minh
*/
public class Car implements IInfor{
private String Name, Color;
public Car() {
}
public Car(String Name, String Color) {
this.Name = Name;
this.Color = Color;
}
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.println("Nhap ten:");
Name = input.nextLine();
System.out.println("Nhap mau: ");
Color = input.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 Bt522;
import java.util.ArrayList;
/**
*
* @author Minh
*/
public class Main {
public static void main(String[] args) {
ArrayList<IInfor> 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<IInfor> a){
for (int i = 0; i < a.size(); i++) {
a.get(i).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 Bt522;
/**
*
* @author Minh
*/
public interface IInfor {
void showInfor();
}
![Đỗ Văn Huấn [T1907A]](https://www.gravatar.com/avatar/04c40301dd027839d265b3c3c9dc6e6b.jpg?s=80&d=mm&r=g)
Đỗ Văn Huấn
2020-03-11 15:14:18
package ASSIGNMENT.assignment_60p; |
package ASSIGNMENT.assignment_60p; |
package ASSIGNMENT.assignment_60p; |
package ASSIGNMENT.assignment_60p; |