Java (đọc như "Gia-va") là một ngôn ngữ lập trình dạng lập trình hướng đối tượng (OOP). Khác với phần lớn ngôn ngữ lập trình thông thường, thay vì biên dịch mã nguồn thành mã máy hoặc thông dịch mã nguồn khi chạy, Java được thiết kế để biên dịch mã nguồn thành bytecode, bytecode sau đó sẽ được môi trường thực thi (runtime environment) chạy. Bằng cách này, Java thường chạy chậm hơn những ngôn ngữ lập trình thông dịch khác như C++, Python, Perl, PHP, C#...
2.2: Write an application that reads three integers and prints their average.
Codeimport java.text.NumberFormat;
import java.util.Scanner;
public class bai2 {
static int nhap(String thongbao)
{
int a ;
Scanner scan = new Scanner(System.in);
System.out.println(thongbao);
a = scan.nextInt();
return a;
}
public static void main(String[] args) {
System.out.println("Chuong trinh xuat gia tri trung binh cua 3 so");
// dinh dang lai ket qua lay 3 so thap phan
NumberFormat num = NumberFormat.getNumberInstance();
int a, b, c;
float ketqua;
a = nhap("Ban hay nhap so A");
b = nhap("Ban hay nhap so B");
c = nhap("Ban hay nhap so C");
ketqua = Trungbinh(a, b, c);
System.out.println("Ket qua cua 3 so la: " + num.format(ketqua));
}
static float Trungbinh(int a, int b, int c)
{
float kq = (float) (a+b+c)/3;
return kq;
}
}
2.3 Write an application that reads two floating point numbers and prints their sum, difference, and product.
codeimport java.text.NumberFormat;
import java.util.Scanner;
public class bai3 {
static float nhap(String thongbao)
{
float x ;
Scanner scan = new Scanner(System.in);
System.out.println(thongbao);
x = scan.nextFloat();
return x;
}
public static void main(String[] args) {
System.out.println("Chuong trinh ting tong, tich va thuong cua 2 so nhap vao");
// dinh dang lai ket qua lay 3 so thap phan
NumberFormat num = NumberFormat.getNumberInstance();
float a, b;
float ketqua;
a = nhap("Ban hay nhap so A");
b = nhap("Ban hay nhap so B");
ketqua = tong(a, b);
System.out.println("Tong cua 2 so la: " + ketqua );
ketqua = tich(a, b);
System.out.println("Tich cua 2 so la: " + ketqua );
ketqua = thuong(a, b);
System.out.println("Thuong cua 2 so la: " + ketqua );
}
static float tong(float a, float b){
float _tong = a + b;
return _tong;
}
static float tich(float a, float b){
float _tich = a * b;
return _tich;
}
static float thuong(float a, float b){
float _thuong = a / b;
return _thuong;
}
}
2.4 Create a revised version of the TempConverter application to con-vert from Fahrenheit to Celsius. Read the Fahrenheit temperature from the user.
code/////////// chuyen do C qua F
import java.util.Scanner;
public class bai4a {
static int nhap(String thongbao)
{
int x ;
Scanner scan = new Scanner(System.in);
System.out.println(thongbao);
x = scan.nextInt();
return x;
}
public static void main(String[] args) {
System.out.println("Chuong trinh chuyen doi do C qua F");
int a;
a = nhap("Ban hay nhap do C");
ConverF(a);
}
//******************** chuyen độ C qua F
static void ConverF(int C)
{
final int BASE = 32;
final double CONVERSION_FACTOR = 9.0 / 5.0;
int celsiusTemp = C; // value to convert
double fahrenheitTemp;
fahrenheitTemp = celsiusTemp * CONVERSION_FACTOR + BASE;
System.out.println ("Do C ban nhap la : " + celsiusTemp);
System.out.println ("Tuong ung voi do F la: " + fahrenheitTemp);
}
}
**/////////// chuyen do C qua F
import java.util.Scanner;
public class bai4b {
static int nhap(String thongbao)
{
int x ;
Scanner scan = new Scanner(System.in);
System.out.println(thongbao);
x = scan.nextInt();
return x;
}
public static void main(String[] args) {
System.out.println("Chuong trinh chuyen doi do C qua F");
int a;
a = nhap("Ban hay nhap do F");
ConverF(a);
}
//******************** chuyen độ F qua C
static void ConverF(int F)
{
final int BASE = 32;
final double CONVERSION_FACTOR = 5.0 / 9.0;
int fahrenheitTemp = F;
double celsiusTemp; // value to convert
celsiusTemp = (fahrenheitTemp - BASE) * CONVERSION_FACTOR;
System.out.println ("Do F ban nhap la : " + fahrenheitTemp);
System.out.println ("Do C tuong ung la : " + celsiusTemp);
}
}
2.5 Write an application that converts miles to kilometers. (One mile equals 1.60935 kilometers.) Read the miles value from the user as a floating point value.
codepublic class bai5 {
static float nhap(String thongbao)
{
float x ;
Scanner scan = new Scanner(System.in);
System.out.println(thongbao);
x = scan.nextFloat();
return x;
}
public static void main(String[] args) {
System.out.println("Chuong trinh chuyen doi gio, phut, giay");
float a;
a = nhap("Ban hay Mile");
System.out.println("Tuong ung " + a + " Miles thi bang " + ConverKm(a));
}
static float ConverKm(float a)
{
// 1 m thi bang 1.60935 km
//Ep kieu du lieu
float kilomiles = (float) a * 1.60935F;
return kilomiles;
}
}
2.6 Write an application that reads values representing a time duration in hours, minutes, and seconds, and then print the equivalent total number of seconds. (For example, 1 hour, 28 minutes, and 42 sec-onds is equivalent to 5322 seconds.)
codeimport java.util.Scanner;
public class bai2 {
static int nhap(String thongbao)
{
int x ;
Scanner scan = new Scanner(System.in);
System.out.println(thongbao);
x = scan.nextInt();
return x;
}
public static void main(String[] args) {
System.out.println("Chuong trinh chuyen doi Miles qua Kilomiles");
int gio, phut, giay;
float tonggiay;
gio = nhap("Ban hay gio: ");
phut = nhap("Ban hay phut: ");
giay = nhap("Ban hay giay: ");
tonggiay = TongGiay(gio, phut, giay);
System.out.printf("Ban nhap thoi gian la: %d:%d:%d = %f giay", gio, phut, giay, tonggiay);
}
static float TongGiay(int a, int b, int c)
{
int gio = a*60*60;
int phut = b * 60;
// do c la giay nen trong cong thang vao luon
float giay = gio + phut + c;
return giay;
}
}
2.7 Create a revised version of the previous project that reverses the computation. That is, read a value representing a number of sec-onds, then print the equivalent amount of time as a combination of hours, minutes, and seconds. (For example, 9999 seconds is equiva-lent to 2 hours, 46 minutes, and 39 seconds.)
codeimport java.util.Scanner;
public class bai2 {
static int nhap(String thongbao)
{
int x ;
Scanner scan = new Scanner(System.in);
System.out.println(thongbao);
x = scan.nextInt();
return x;
}
public static void main(String[] args) {
System.out.println("Chuong trinh chuyen doi gio, phut, giay");
int giay;
giay = nhap("Ban hay giay: ");
ConverHMS(giay);
}
static void ConverHMS(int n)
{
int gio = n / 3600;
int phut = (n / 60)%60;
int giay = n%60;
System.out.printf("Ban nhap so giay la %d ung voi %d:%d:%d", giay, gio, phut, giay);
}
}
2.8 Write an application that reads the (x,y)coordinates for twopoints. Compute the distance between the two points using the fol-lowing formula:
Code
import java.util.Scanner;
public class bai8 {
static double nhap(String thongbao)
{
double x ;
Scanner scan = new Scanner(System.in);
System.out.println(thongbao);
x = scan.nextDouble();
return x;
}
public static void main(String[] args) {
System.out.println("Chuong trinh chuyen doi gio, phut, giay");
double x1, x2, y1, y2;
double khoangcach;
x1 = nhap("Ban hay gia tri X1: ");
x2 = nhap("Ban hay gia tri X2: ");
y1 = nhap("Ban hay gia tri Y1: ");
y2 = nhap("Ban hay gia tri Y2: ");
khoangcach = Distance(x1, x2, y1, y2);
System.out.println("Khoang cach giua 2 diem X va Y la: " + khoangcach);
}
static double Distance(double x1, double x2, double y1, double y2)
{
double kc ;
//binh Phuong la su dung Math.pow
double X = Math.pow((x2-x1), 2);
double Y = Math.pow((y2+y1), 2);
//Can la su dung Math.sqrt
kc = Math.sqrt(X+Y);
return kc;
}
}
Code
import java.text.DecimalFormat;
import java.util.Scanner;
public class bai2 {
static double nhap(String thongbao)
{
double x ;
Scanner scan = new Scanner(System.in);
System.out.println(thongbao);
x = scan.nextDouble();
return x;
}
public static void main(String[] args) {
System.out.println("Chuong trinh tinh khoi luong va dien tich hinh cau");
// dinh dang kieu du lieu
DecimalFormat dfmat = new DecimalFormat("0.00");
double kl, dt, radius;
radius = nhap("Ban hay nhap ban kinh: ");
//*********** tinh khoi luong
kl = volume(radius);
System.out.println("Khoi luong hinh cau: " + dfmat.format(kl));
//*********** tinh dien tich
dt = SurfaceArea(radius);
System.out.println("Dien tich hinh cau: " + dfmat.format(dt));
}
static double volume(double radius)
{
double khoiluong;
khoiluong = (4.0/3.0) * Math.PI * Math.pow(radius, 3);
return khoiluong ;
}
static double SurfaceArea(double radius)
{
double dientich;
dientich = 4.0 * Math.PI * Math.pow(radius, 2);
return dientich;
}
}
2.10 Write an application that reads the lengths of the sides of a triangle from the user. Compute the area of the triangle using Heron’s formu-la (below), in which s represents half of the perimeter of the triangle, and a, b, and c represent the lengths of the three sides. Print the area to three decimal places.
Code
import java.text.DecimalFormat;
import java.util.Scanner;
public class bai10 {
static double nhap(String thongbao)
{
double x ;
Scanner scan = new Scanner(System.in);
System.out.println(thongbao);
x = scan.nextDouble();
return x;
}
public static void main(String[] args) {
System.out.println("Chuong trinh tinh dien tich cua 1 tam giac");
// dinh dang kieu du lieu
DecimalFormat dfmat = new DecimalFormat("0.0000");
double a, b, c, Area;
a = nhap("Ban hay nhap goc A");
b = nhap("Ban hay nhap goc B");
c = nhap("Ban hay nhap goc C");
Area = Dientich(a, b, c);
System.out.println("Chu vi cua tam giac la: " + dfmat.format(Area));
}
static double Dientich(double a, double b, double c)
{
double area = 0, s ;
if (a > 0 && b > 0 && c >0) {
if ((a+b) > c && (b+c) > a && (c+a) > b) {
s = (a + b + c)/2;
area = Math.sqrt((s*(s - a)*(s - b)*(s - c)));
}
else
System.out.println("Cac canh cua tam giac phai lon hon mot goc");
}
else
System.out.println("Cac goc cua tam giac phai lon hon 0");
return area;
}
}
2.11 Write an application that computes the number of miles per gallon (MPG) of gas for a trip. Accept as input a floating point number that represents the total amount of gas used. Also accept two inte-gers representing the odometer readings at the start and end of the trip. Compute the number of kilometers per liter if you prefer.
Code:
/*----------------------- GALLON | KILOMETERS ---------------------------------*/
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;
import java.util.Scanner;
public class MPG_KLM {
static int nhap(String thongbao)
{
int x ;
Scanner scan = new Scanner(System.in);
System.out.println(thongbao);
x = scan.nextInt();
return x;
}
public static void main(String[] args)
{
System.out.println("Chương trình xuất ra số Gallon (MPG) cho 1 chiến đi du lịch.");
int startMile, endMile, Miles, gal, klm , sel;
DecimalFormat num = new DecimalFormat("0.0000");
double MPG, KLM;
// Người dùng chọn lựa theo chương trình
// 0 thì tính MPG
// Khác 0 thì tính kilometers
sel = nhap("Bạn hãy muốn tính theo MPG(0) hay KLM(!0):");
switch (sel) {
case 0:
startMile = nhap("Bạn hãy nhập số dặm bắt đầu:");
endMile = nhap("Bạn hãy nhập số dặm sau:");
// Ra được số dặm
Miles = Miles(startMile, endMile);
gal = nhap("Bạn hãy nhập số Gallon");
// Goi hàm tính toán MPG
MPG = CaculatorMpg(Miles, gal);
System.err.println("Chiến du lịch này bạn phải tốn hết: " + num.format(MPG) + " nguyên liệu.");
break;
default:
klm = nhap("Bạn hãy nhập số kilomet");
gal = nhap("Bạn hãy nhập số Gallon");
// Goi hàm tính toán MPG
KLM = CaculatorKM(klm, gal);
System.err.println("Số liters bạn đỗ đủ đi được trong chiến du lịch là: " + num.format(KLM) + " cây số.");
break;
}
}
// Tinh so dam
static int Miles(int StartM, int EndM)
{
int M = EndM - StartM;
System.out.println("Số dặm mà xe bạn chạy là: " + M);
return M;
}
static double CaculatorMpg(int miles, int gallon){
//MPG = số dặm chia cho gallon
double mpg = (double) miles / gallon;
return mpg;
}
//**//********************************
// Tinh so km neu ban biet lit.
static double CaculatorKM(int km, int Gal)
{
//http://www.asknumbers.com/GallonsToLitersConversion.aspx
// 1 Gallon = 3.7854118 liters
//final double Gallon = 3.7854118;
double liters = 3.7854118 * Gal;
System.out.println("Số km là : " + km);
System.out.println("Số liters là : " + liters);
double klm = (double) km /liters;
return klm;
}
}
2.12 Write an application that determines the value of the coins in a jar and prints the total in dollars and cents. Read integer values that represent the number of quarters, dimes, nickels, and pennies. Use a currency formatter to print the output.
Code:
/*----------------------- TOTAL DOLLARS AND CENTS---------------------------------*/
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;
import java.util.Scanner;
public class TotalDollarsAndCents {
static int nhap(String thongbao)
{
int x ;
Scanner scan = new Scanner(System.in);
System.out.println(thongbao);
x = scan.nextInt();
return x;
}
public static void main(String[] args)
{
System.out.println("Chương trình xuất ra giá trị đồng xu.");
int q, d, n, p;
q = nhap("Bạn hãy nhập giá trị quarters = 25 cents");
d = nhap("Bạn hãy nhập giá trị dimes = 10 cents");
n = nhap("Bạn hãy nhập giá trị nickels = 5 cents");
p = nhap("Bạn hãy nhập giá trị pennies = 1 cents");
totalindollarsandcents(q, d, n, p);
}
static double quarters(int n){
double quarters = 0.25 * n; // tương ứng với 25 cents
return quarters;
}
static double dimes(int n)
{
double dimes = 0.1 * n; // tương ứng với 10 cents
return dimes;
}
static double nickels(int n)
{
double nickels = 0.05 * n; // tương ứng với 5 cents
return nickels;
}
static double pennies(int n)
{
double pennies = 0.01 * n; // pennies = cents | 1 cents
return pennies;
}
static void totalindollarsandcents(int q, int d, int n, int p)
{
int total, cent;
total = (int) quarters(q) + (int) dimes(d) + (int) nickels(n) + (int) pennies(p);
cent = 100 % total;
System.out.println("Bạn đã có " + total + " đôla và " + cent + " xu.");
}
}
2.13 Write an application that creates and prints a random phone number of the form XXX-XXX-XXXX. Include the dashes in the output. Do not let the first three digits contain an 8 or 9 (but don ’t be more restric-tive than that), and make sure that the second set of three digits is not greater than 742. Hint:Think through the easiest way to con-struct the phone number. Each digit does not have to be determined separately.
Code: /*----------------------- DIGITAL NUMBER PHONE ---------------------------------*/
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;
import java.util.Scanner;
public class DigitalNumber{
static int nhap(String thongbao)
{
int x ;
Scanner scan = new Scanner(System.in);
System.err.println(thongbao);
x = scan.nextInt();
return x;
}
public static void main(String[] args)
{
System.out.println("Chuong trinh xuat ra so dien thoai");
int n;
n = nhap("Ban hay nhap so luong so dien thoat can xuat");
xuat(n);
}
static void xuat(int n)
{
for (int i = 0; i < n; i++) {
DigitalPhone();
}
}
static void DigitalPhone()
{
Random rad = new Random();
String First = "", last = "", mid="";
int so = 0;
//Day so dau
for (int i = 0; i < 3; i++) {
so = rad.nextInt(8);
First += so;
}
// Day so thu 2
so = rad.nextInt(742);
do {
mid = "";
for (int i = 0; i < 3; i++) {
so = rad.nextInt(7);
mid += so;
}
} while (so > 742);
//Day so cuoi
for (int i = 0; i < 4; i++) {
so = rad.nextInt(8);
last += so;
}
String xuat = First + "-" + mid + "-" + last;
System.out.println("Digital Phone: " + xuat);
}
}
0 nhận xét:
Đăng nhận xét