1. Quản lý bán hàng dùng kế thừa trong java
2. Tính điểm môn học dùng kế thừa trong javapackage baitap_coban;import java.util.Scanner;class Laptop{public String tenhang,mahang;public int giahang,soluong, giamgia;public static float Tinhtien(int giahang, int soluong) {return (float) giahang*soluong;}public static float Tinhtien(int giahang, int soluong, int giamgia){int giagiam = (giahang*soluong*giamgia)/100;return (float) (giahang*soluong)-giagiam;}private Scanner in;//-----------------------------------------------------------//public void Nhap(){in = new Scanner(System.in);System.out.print("Ma Hang: ");mahang = in.nextLine();System.out.print("Ten Hang: ");tenhang = in.nextLine();System.out.print("So Luong: ");soluong = in.nextInt();System.out.print("Gia Hang: ");giahang = in.nextInt();System.out.print("Giam Gia: ");giamgia = in.nextInt();System.out.println();}public void Show(){System.out.println("Ma Hang: "+mahang);System.out.println("Ten Hang: "+tenhang);System.out.println("Gia Hang: "+giahang);System.out.println("So Luong: "+soluong);System.out.println("Tong tien: "+this.Tinhtien(giahang, soluong));System.out.println();}}//------------------------------------------------------------//class Dell extends Laptop{public static float Tinhtien(int giahang, int soluong, int giamgia) {int giagiam = (giahang*soluong*(giamgia+10))/100;return (float) (giahang*soluong)-giagiam;}}public class QuanLyBanHang {public static void main(String[] args) {Laptop a = new Laptop();a.Nhap();a.Show();Dell b = new Dell();b.Nhap();b.Show();}}
package baitap_coban;
import java.util.Scanner;
import java.io.*;
class SVien {
String TenSV;
int MaSV;
void setSV(String Ten, int Ma){
TenSV=Ten;
MaSV=Ma;
}
public void viewSV(){
System.out.println ("Ten Sinh Vien: " +TenSV);
System.out.println ("Ma Sinh Vien: " +MaSV);
}
}
class SinhvienCNTT extends SVien {
float dtoan,dtin;
public void setSVCNTT(String TenSV,int MaSV, float dtoan, float dtin){
super.setSV(TenSV,MaSV);
this.dtoan=dtoan;
this.dtin=dtin;
}
public void viewSVCNTT(){
super.viewSV();
System.out.println ("Diem Toan: " +this.dtoan);
System.out.println ("Diem Tin: " +this.dtin);
System.out.println ("Tong Diem: "+this.tongdiem());
}
public float tongdiem(){
return dtoan+dtin;
}
public static void main (String[] args) {
String TenSV;
int MaSV;
float dtoan,dtin;
Scanner input = new Scanner(System.in);
SinhvienCNTT a[] = new SinhvienCNTT[50];
System.out.print ("Nhap so sinh vien n= ");
int n = input.nextInt();
for(int i=0;i<n;i++){
a[i]=new SinhvienCNTT();
System.out.println ("Nhap a["+i+"]: ");
System.out.println ("Ten SV: ");
input = new Scanner(System.in);
TenSV = input.nextLine();
System.out.println ("Ma SV: ");
MaSV = input.nextInt();
System.out.println ("Diem toan: ");
dtoan = input.nextFloat();
System.out.println ("Diem tin; ");
dtin = input.nextFloat();
a[i].setSVCNTT(TenSV,MaSV,dtoan,dtin);
}
System.out.println ("Sinh Vien Co Tong Diem > 10 la: ");
for (int i=0; i< n; i++){
if(a[i].tongdiem()>10){
a[i].viewSVCNTT();
}
}
System.out.println ("Sinh Vien Co Tong Diem < 5 la: ");
for (int i = 0; i< n; i++){
if(a[i].tongdiem()<5){
a[i].viewSVCNTT();
}
}
}
}3. Số hoàn hảo trong java
package baitap_coban;
import java.util.Scanner;
public class So_hoan_hao {
public static void main(String[]args) {
int i, j, n;
Scanner nhap = new Scanner(System.in);
System.out.print("nhap vao n: ");
n = nhap.nextInt();
System.out.println("Day so hoan hao la:");
for (i = 1; i <= n; i++) {
int tong = 1;
for (j = 2; j <= i / 2; j++) {
if (i % j == 0)
tong = tong + j;
}
if (tong == i)
System.out.print(" " + i );
}
}
}
0 comments:
Post a Comment