- Xây dựng interface Tinh1, Tinh2 như sau:
interface Tinh1 {
void Nhap();long GiaiThua(int n);
}
// interface Tinh2 kế thừa Tính1
interface Tinh2 extends Tinh1 {
float S1(int n);
float S2(int n);
float S3(int n, float x);
}
float S1(int n);
float S2(int n);
float S3(int n, float x);
}
- Xây dựng class TinhTong implements Tinh2, trong đó:
S1 = 1!+2!+3!+...+n!
S2 = 2!+4!+6!+...+(2n)!
S3 = x^1 / 1 + x^2 / 2 + x^3 / 3 +...+ x^n / n
(1 < x < 5; 1 < n < 100, nhập vào từ bàn phím)
[Code Java]
/* Sử dụng interface */
package interface_vd1;
import java.util.Scanner;
/************interface Tinh1****************/
interface Tinh1 {
void Nhap();
long GiaiThua(int n);
}
[Code Java]
/* Sử dụng interface */
package interface_vd1;
import java.util.Scanner;
/************interface Tinh1****************/
interface Tinh1 {
void Nhap();
long GiaiThua(int n);
}
/***********interface Tinh2****************/
interface Tinh2 extends Tinh1 {
float S1(int n);
float S2(int n);
float S3(int n, float x);
}
/************class TinhTong*****************/
class TinhTong implements Tinh2 {
private int n;
private float x;
interface Tinh2 extends Tinh1 {
float S1(int n);
float S2(int n);
float S3(int n, float x);
}
/************class TinhTong*****************/
class TinhTong implements Tinh2 {
private int n;
private float x;
// Set x (thiet lap x)
public void Set_x(float x) {
this.x = x;
}
// get x (lay gia tri x)
public float Get_x() {
return x;
}
// Set n (thiet lap n)
public void Set_n(int n) {
this.n = n;
}
// get n (lay gia tri n)
public int Get_n() {
return n;
}
public void Set_x(float x) {
this.x = x;
}
// get x (lay gia tri x)
public float Get_x() {
return x;
}
// Set n (thiet lap n)
public void Set_n(int n) {
this.n = n;
}
// get n (lay gia tri n)
public int Get_n() {
return n;
}
// Nhap
public void Nhap() {
Scanner in = new Scanner(System.in);
// nhap n
int t = 0;
System.out.print("\n - Nhap n: ");
try {
do {
System.out.print("\n n = ");
t = in.nextInt();
if (t < 1 || t > 100) {
System.out.print("\n Nhap lai n!");
}
} while (t < 1 || t > 100);
} catch (Exception ex) {
System.out.print("\n *** Da co loi " + ex.toString() + " khi nhap n!");
}
Set_n(t);
public void Nhap() {
Scanner in = new Scanner(System.in);
// nhap n
int t = 0;
System.out.print("\n - Nhap n: ");
try {
do {
System.out.print("\n n = ");
t = in.nextInt();
if (t < 1 || t > 100) {
System.out.print("\n Nhap lai n!");
}
} while (t < 1 || t > 100);
} catch (Exception ex) {
System.out.print("\n *** Da co loi " + ex.toString() + " khi nhap n!");
}
Set_n(t);
// nhap x
float m = 0;
System.out.print("\n - Nhap x: ");
try {
do {
System.out.print("\n x = ");
m = in.nextFloat();
if (m < 1 || m > 5) {
System.out.print("\n Nhap lai x!");
}
} while (m < 1 || m > 5);
} catch (Exception ex) {
System.out.print("\n *** Da co loi " + ex.toString() + " khi nhap x!");
}
Set_x(m);
}
// GiaiThua
public long GiaiThua(int n) {
long gt = 1;
n = Get_n();
for (int i = 1; i <= n; i++) {
gt += i;
}
return gt;
}
// S1
public float S1(int n) {
float s1 = 0;
n = Get_n();
for (int i = 1; i <= n; i++) {
s1 += GiaiThua(i);
}
return s1;
}
// S2
public float S2(int n) {
float s2 = 0;
n = Get_n();
for (int i = 2; i <= (2 * n); i = i + 2) {
s2 += GiaiThua(i);
}
return s2;
}
// S3
public float S3(int n, float x) {
float s3 = 0, tu = 1;
n = Get_n();
x = Get_x();
for (int i = 1; i <= n; i++) {
tu *= x;
s3 += tu / i;
}
return s3;
}
// In ket qua
void InKQ() {
System.out.print("\n * KET QUA: ");
System.out.print("\n + S1= " + S1(n));
System.out.print("\n + S2= " + S2(n));
System.out.print("\n + S3= " + S3(n, x));
}
}
/************class main*************************/
public class Interface_vd1 {
public static void main(String[] args) {
TinhTong t = new TinhTong();
t.Nhap();
t.InKQ();
}
}
float m = 0;
System.out.print("\n - Nhap x: ");
try {
do {
System.out.print("\n x = ");
m = in.nextFloat();
if (m < 1 || m > 5) {
System.out.print("\n Nhap lai x!");
}
} while (m < 1 || m > 5);
} catch (Exception ex) {
System.out.print("\n *** Da co loi " + ex.toString() + " khi nhap x!");
}
Set_x(m);
}
// GiaiThua
public long GiaiThua(int n) {
long gt = 1;
n = Get_n();
for (int i = 1; i <= n; i++) {
gt += i;
}
return gt;
}
// S1
public float S1(int n) {
float s1 = 0;
n = Get_n();
for (int i = 1; i <= n; i++) {
s1 += GiaiThua(i);
}
return s1;
}
// S2
public float S2(int n) {
float s2 = 0;
n = Get_n();
for (int i = 2; i <= (2 * n); i = i + 2) {
s2 += GiaiThua(i);
}
return s2;
}
// S3
public float S3(int n, float x) {
float s3 = 0, tu = 1;
n = Get_n();
x = Get_x();
for (int i = 1; i <= n; i++) {
tu *= x;
s3 += tu / i;
}
return s3;
}
// In ket qua
void InKQ() {
System.out.print("\n * KET QUA: ");
System.out.print("\n + S1= " + S1(n));
System.out.print("\n + S2= " + S2(n));
System.out.print("\n + S3= " + S3(n, x));
}
}
/************class main*************************/
public class Interface_vd1 {
public static void main(String[] args) {
TinhTong t = new TinhTong();
t.Nhap();
t.InKQ();
}
}
[Download Project Tại đây]
0 nhận xét:
Đăng nhận xét