Java Basic- Overview - Tổng hợp bài tập ôn luyện java basic - Kiểm tra 60 phút
Bài 1: Tính tổng các số nguyên từ 0->N (N được nhập từ bàn phím)
Bài 2: Nhập vào mảng số nguyên gồm N phần tử (N được nhập từ bàn phím) -> In ra các số chia hết cho 2
Bài 3 : Tạo lớp đối tượng Book gồm các thuộc tính sau
- tên sách, tác giả, nhà sản xuất, năm sản xuất
-Viết getter/setter và 2 hàm tạo
- Viết hàm nhập/hàm hiển thị
- Tạo 2 đối tượng Book với 2 hàm tạo tương ứng trong phương thức main của lớp test. Nhập và hiển thị thông tin sách
Bài 4 :
Tạo lớp People gồm các thuộc tính : tên, địa chỉ
- tạo hàm nhập và hiển thị thông tin sinh viên
Tạo lớp Student kế thừa từ lớp People và có thêm các thuộc tính rollNo
- Ghi đè 2 phương thức nhập và hiển thị thông tin sinh viên
Tạo 2 đối tượng People và Student trong phương thức main của lớp Test. Thực hiện nhập và hiển thị thông tin của 2 đối tượng trên.
Bài 5:
Tạo lớp Vehicle gồm 3 phương thức abstract : running, input, display
Tạo lớp Car kế thừa từ lớp Vehicle có thuộc tính name, số bánh xe.
- Viết các hàm input và display và running (hàm running sẽ hiển thị dòng Car is running)
Tạo lớp BikeCycle kế thừa từ lớp Vehicle có thuộc tính name, số bánh xe
- Viết các hàm input và display và running (hàm running sẽ hiển thị dòng BikeCycle is running)
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![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-11 10:51: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 TongCacSo;
import java.util.Scanner;
/**
*
* @author ASUS
*/
public class main {
public static void main(String[] args) {
int a;
System.out.println("nhap");
Scanner scan=new Scanner(System.in);
a =Integer.parseInt(scan.nextLine());
int sum=0;
while(a!=0){
sum+=a%10;
a/=10;
}
System.out.println("tong"+a+"="+sum);
}
}
/*
* 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 hbgh;
import java.util.Scanner;
/**
*
* @author ASUS
*/
public class main {
public static void main(String[] args) {
int n;
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
if(n%2==0){
System.out.println("so chan");
}else{
System.out.println("so le");
}
}
}
![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-11 10:51:27
/*
* 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 TongCacSo;
import java.util.Scanner;
/**
*
* @author ASUS
*/
public class main {
public static void main(String[] args) {
int a;
System.out.println("nhap");
Scanner scan=new Scanner(System.in);
a =Integer.parseInt(scan.nextLine());
int sum=0;
while(a!=0){
sum+=a%10;
a/=10;
}
System.out.println("tong"+a+"="+sum);
}
}
![Luong Dinh Dai [T1907A]](https://www.gravatar.com/avatar/ca08fa4090e1038e541197564747f93c.jpg?s=80&d=mm&r=g)
Luong Dinh Dai
2020-03-11 10:32:36
/*
* 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 bai5;
import java.util.Scanner;
/**
*
* @author FATE STORE
*/
public class BikeCycle extends Vehicle{
String name;
int sobanh;
public BikeCycle(String name, int sobanh) {
this.name = name;
this.sobanh = sobanh;
}
public BikeCycle() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSobanh() {
return sobanh;
}
public void setSobanh(int sobanh) {
this.sobanh = sobanh;
}
@Override
public String toString() {
return "BikeCycle{" + "name=" + name + ", sobanh=" + sobanh + '}';
}
@Override
public void running() {
System.out.println("BikeCycle is running");
}
@Override
public void input() {
System.out.println("Nhap ten: ");
Scanner sc = new Scanner(System.in);
name= sc.nextLine();
System.out.println("So banh: ");
sobanh = sc.nextInt();
sc.nextLine();
}
@Override
public void display() {
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 bai5;
import java.util.Scanner;
/**
*
* @author FATE STORE
*/
public class Car extends Vehicle{
String name;
int sobanh;
public Car() {
}
public Car(String name, int sobanh) {
this.name = name;
this.sobanh = sobanh;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSobanh() {
return sobanh;
}
public void setSobanh(int sobanh) {
this.sobanh = sobanh;
}
@Override
public String toString() {
return "Car{" + "name=" + name + ", sobanh=" + sobanh + '}';
}
@Override
public void running() {
System.out.println("Car is running");
}
@Override
public void input() {
System.out.println("Nhap ten xe: ");
Scanner sc = new Scanner(System.in);
name = sc.nextLine();
System.out.println("So banh xe: ");
sobanh = sc.nextInt();
sc.hasNextLine();
}
@Override
public void display() {
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 bai5;
/**
*
* @author FATE STORE
*/
public abstract class Vehicle {
public abstract void running();
public abstract void input();
public abstract void display();
}
/*
* 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 bai5;
/**
*
* @author FATE STORE
*/
public class Main {
public static void main(String[] args) {
Car car = new Car();
BikeCycle bike = new BikeCycle();
car.input();
car.running();
car.display();
bike.input();
bike.display();
bike.running();
}
}
![Đỗ 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 10:31:59
BAI1 package BaiTapNgay11_3_2020.OnLuyenKt60p; |
BAI2 package BaiTapNgay11_3_2020.OnLuyenKt60p; |
BAI3 package BaiTapNgay11_3_2020.OnLuyenKt60p.Bai3; package BaiTapNgay11_3_2020.OnLuyenKt60p.Bai3; |
BAI4 package BaiTapNgay11_3_2020.OnLuyenKt60p.Bai4; package BaiTapNgay11_3_2020.OnLuyenKt60p.Bai4; package BaiTapNgay11_3_2020.OnLuyenKt60p.Bai4; |
BAI5 package BaiTapNgay11_3_2020.OnLuyenKt60p.Bai5; package BaiTapNgay11_3_2020.OnLuyenKt60p.Bai5; package BaiTapNgay11_3_2020.OnLuyenKt60p.Bai5; package BaiTapNgay11_3_2020.OnLuyenKt60p.Bai5; |
![Luong Dinh Dai [T1907A]](https://www.gravatar.com/avatar/ca08fa4090e1038e541197564747f93c.jpg?s=80&d=mm&r=g)
Luong Dinh Dai
2020-03-11 10:31:49
/*
* 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 bai4;
/**
*
* @author FATE STORE
*/
public class Main {
public static void main(String[] args) {
People peo = new People();
Student stu = new Student();
peo.input();
peo.output();
stu.input();
stu.output();
}
}
/*
* 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 bai4;
import java.util.Scanner;
/**
*
* @author FATE STORE
*/
public class People {
String ten,diachi;
public People() {
}
public People(String ten, String diachi) {
this.ten = ten;
this.diachi = diachi;
}
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;
}
@Override
public String toString() {
return "People{" + "ten=" + ten + ", diachi=" + diachi + '}';
}
public void input(){
Scanner sc = new Scanner(System.in);
System.out.println("Nhap ten: ");
ten = sc.nextLine();
System.out.println("Nhap dia chi: ");
diachi = sc.nextLine();
}
public void output(){
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 bai4;
import java.util.Scanner;
/**
*
* @author FATE STORE
*/
public class Student extends People{
int rollNo;
@Override
public void input(){
super.input();
Scanner sc = new Scanner(System.in);
System.out.println("Nhap so tt: ");
rollNo = sc.nextInt();
}
@Override
public void output(){
super.output();
System.out.println("rollNo"+rollNo);
}
public Student(int rollNo) {
this.rollNo = rollNo;
}
public Student(int rollNo, String ten, String diachi) {
super(ten, diachi);
this.rollNo = rollNo;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
@Override
public String getTen() {
return ten;
}
@Override
public void setTen(String ten) {
this.ten = ten;
}
@Override
public String getDiachi() {
return diachi;
}
@Override
public void setDiachi(String diachi) {
this.diachi = diachi;
}
public Student() {
}
}
![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-11 10:31: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 TongCacSo;
import java.util.Scanner;
/**
*
* @author ASUS
*/
public class main {
public static void main(String[] args) {
int a;
System.out.println("nhap");
Scanner scan=new Scanner(System.in);
a =Integer.parseInt(scan.nextLine());
int sum=0;
while(a!=0){
sum+=a%10;
a/=10;
}
System.out.println("tong"+a+"="+sum);
}
}
![Luong Dinh Dai [T1907A]](https://www.gravatar.com/avatar/ca08fa4090e1038e541197564747f93c.jpg?s=80&d=mm&r=g)
Luong Dinh Dai
2020-03-11 10:31:15
b3
/*
* 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 bai3;
import java.util.Scanner;
/**
*
* @author FATE STORE
*/
public class Book {
String tensach, tacgia, nhasx;
int namsx;
Scanner sc = new Scanner(System.in);
public Book() {
}
public Book(String tensach, String tacgia, String nhasx, int namsx) {
this.tensach = tensach;
this.tacgia = tacgia;
this.nhasx = nhasx;
this.namsx = namsx;
}
public String getTensach() {
return tensach;
}
public void setTensach(String tensach) {
this.tensach = tensach;
}
public String getTacgia() {
return tacgia;
}
public void setTacgia(String tacgia) {
this.tacgia = tacgia;
}
public String getNhasx() {
return nhasx;
}
public void setNhasx(String nhasx) {
this.nhasx = nhasx;
}
public int getNamsx() {
return namsx;
}
public void setNamsx(int namsx) {
this.namsx = namsx;
}
@Override
public String toString() {
return "Book{" + "tensach=" + tensach + ", tacgia=" + tacgia + ", nhasx=" + nhasx + ", namsx=" + namsx + '}';
}
public void input(){
System.out.println("Nhap ten tac gia: ");
tacgia = sc.nextLine();
System.out.println("Nhap ten sach: ");
tensach = sc.nextLine();
System.out.println("Nhap nha san xuat: ");
nhasx =sc.nextLine();
System.out.println("Nhap nam san xuat: ");
namsx = sc.nextInt();
}
public void output(){
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 bai3;
/**
*
* @author FATE STORE
*/
public class Main {
public static void main(String[] args) {
Book book = new Book();
Book book1 = new Book();
book.input();
book1.input();
book.output();
book1.output();
}
}
![Luong Dinh Dai [T1907A]](https://www.gravatar.com/avatar/ca08fa4090e1038e541197564747f93c.jpg?s=80&d=mm&r=g)
Luong Dinh Dai
2020-03-11 10:30:48
b2
/**
*
* @author FATE STORE
*/
public class Main {
public static void main(String[] args) {
int n;
System.out.println("Nhap vao so phan tu: ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int [] array = new int[n];
for(int i =0;i<n;i++){
System.out.printf("Nhap vao phan tu thu %d: ",(i+1));
array[i] = sc.nextInt();
sc.nextLine();
}
for(int i = 0;i<n;i++){
if(array[i]%2==0){
System.out.println(" "+array[i]);
}
}
}
}
![Luong Dinh Dai [T1907A]](https://www.gravatar.com/avatar/ca08fa4090e1038e541197564747f93c.jpg?s=80&d=mm&r=g)
Luong Dinh Dai
2020-03-11 10:30:10
b1
/*
* 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 bai1;
import java.util.Scanner;
/**
*
* @author FATE STORE
*/
public class Main {
public static void main(String[] args) {
int n;
int tong= 0;
System.out.println("Nhap n: ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(int i = 0; i<=n;i++){
tong = tong +i;
}
System.out.println("Tong 0 -> N: "+tong);
}
}
![Lê Minh Bắc [T1907A]](https://www.gravatar.com/avatar/5ce1edab2b287d2eeae03d307a8d4ae9.jpg?s=80&d=mm&r=g)
Lê Minh Bắc
2020-03-11 10:22:12
Bài 1:
package BT997.Bai1;
import java.util.Scanner;
public class B1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int tong = 0;
System.out.print("Nhap vao n:");
int n = Integer.parseInt(scan.nextLine());
for (int i = 1; i <= n; i++){
tong += i;
}
System.out.println("Tong cac so tu 0 den " + n + " = " + tong);
}
}
Bài 2:
package BT997.Bai2;
import java.util.Scanner;
public class B2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Nhap vao n:");
int n = Integer.parseInt(scan.nextLine());
int[] t = new int[n];
for (int i = 0; i < n; i++){
System.out.print("Nhap vao so thu " + (i+1) + ":" );
t[i] = Integer.parseInt(scan.nextLine());
}
System.out.print("Cac so chia het cho 2 la: " );
for (int i = 0; i < n; i++){
if (t[i] % 2 == 0)
System.out.print(t[i] + " ");
}
}
}
Bài 3:
Book:
package BT997.Bai3;
import java.util.Scanner;
public class Book {
String bookName;
String authorName;
String producer;
int year;
public Book() {
}
public Book(String bookName, String authorName, String producer, int year) {
this.bookName = bookName;
this.authorName = authorName;
this.producer = producer;
this.year = year;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public String getProducer() {
return producer;
}
public void setProducer(String producer) {
this.producer = producer;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public void input(){
Scanner scan = new Scanner(System.in);
System.out.print("Nhập tên sách: ");
bookName = scan.nextLine();
System.out.print("Nhập tên tác giả: ");
authorName = scan.nextLine();
System.out.print("Nhập tên nhà xuất bản: ");
producer = scan.nextLine();
System.out.print("Năm sản xuất: ");
year = Integer.parseInt(scan.nextLine());
}
public void display() {
System.out.println("Tên sách: " + bookName);
System.out.println("Tác Giả: " + authorName);
System.out.println("Nhà xuất bản: " + producer);
System.out.println("Năm sản xuất: " + year);
}
}
Test:
package BT997.Bai3;
public class Test {
public static void main(String[] agrs) {
Book book1 = new Book();
Book book2 = new Book("Lập trình C", "Lê Minh Bắc", "Kim Đồng",120000);
System.out.println("Nhập thông tin sách:");
book1.input();
System.out.println("Thông tin sách 1:");
book1.display();
System.out.println("Thông tin sách 2:");
book2.display();
}
}
Bài 4:
People:
package BT997.Bai4;
import java.util.Scanner;
public class People {
String name, address;
public void input(){
Scanner scan = new Scanner(System.in);
System.out.print("Nhập tên : ");
name = scan.nextLine();
System.out.print("Nhập địa chỉ: ");
address = scan.nextLine();
}
public void display() {
System.out.println("Tên: " + name);
System.out.println("Địa chỉ: " + address);
}
}
Student:
package BT997.Bai4;
import java.util.Scanner;
public class Student extends People {
String rollNo;
@Override
public void input(){
Scanner scan = new Scanner(System.in);
super.input();
System.out.print("Nhập RollNo: ");
rollNo = scan.nextLine();
}
@Override
public void display() {
super.display();
System.out.println("RollNo: " + rollNo);
}
}
Test:
package BT997.Bai4;
public class Test {
public static void main(String[] agrs) {
People peo = new People();
Student std = new Student();
System.out.println("Nhập thông tin People:");
peo.input();
System.out.println("Nhập thông tin Student:");
std.input();
System.out.println("Thông tin của People:");
peo.display();
System.out.println("Thông tin của Student:");
std.display();
}
}
Bài 5:
Vehicle:
package BT997.Bai5;
public abstract class Vehicle {
public abstract void running();
public abstract void input();
public abstract void display();
}
Car:
package BT997.Bai5;
import java.util.Scanner;
public class Car extends Vehicle {
String name;
int soBanhXe;
@Override
public void running() {
System.out.print("Car is running!!!");
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.print("Nhập tên xe: ");
name = scan.nextLine();
System.out.print("Nhập số bánh xe: ");
soBanhXe = Integer.parseInt(scan.nextLine());
}
@Override
public void display() {
System.out.println("Tên xe: " + name);
System.out.println("Số bánh xe: " + soBanhXe);
}
}
BikeCycle:
package BT997.Bai5;
import java.util.Scanner;
public class BikeCycle extends Vehicle {
String name;
int soBanhXe;
@Override
public void running() {
System.out.print("BikeCycle is running!!!");
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.print("Nhập tên xe: ");
name = scan.nextLine();
System.out.print("Nhập số bánh xe: ");
soBanhXe = Integer.parseInt(scan.nextLine());
}
@Override
public void display() {
System.out.println("Tên xe: " + name);
System.out.println("Số bánh xe: " + soBanhXe);
}
}
Test:
package BT997.Bai5;
public class Test {
public static void main(String[] agrs) {
Car car = new Car();
BikeCycle bike = new BikeCycle();
System.out.println("Nhập thông tin Car:");
car.input();
System.out.println("Nhập thông tin BikeCycle:");
bike.input();
System.out.println("Thông tin của Car:");
car.display();
System.out.println("Thông tin của BikeCycle:");
bike.display();
}
}