Глава 4
Задача 4.7 "Контроль веса."
Измените задачу 3.3 так, чтобы данные читались с дисплея, а не генерировались как случайные числа.
Решение
Код программы:
import java.io.*;
import java.text.*;
import javagently.*;
import java.util.*;
import java.io.*;
class WeightControl {
NumberFormat num = NumberFormat.getNumberInstance();
WeightControl () {
Man Stive = new Man("Stive",100,98);
Man Jhon = new Man("Jhon",85,98);
//weight
double indexS = Stive.weight;
double indexJ = Jhon.weight;
double indexS1;
double indexJ1;
//volume
double indexS_v = Stive.volume;
double indexJ_v = Jhon.volume;
double indexS1_v;
double indexJ1_v;
boolean done = false;
String status;
Display d = new Display ("WeightControl");
d.prompt("Week",1);
d.prompt("Name 1","Stive Brown");
d.prompt("Weight 1",100);
d.prompt("Volume of weist 1",98);
d.prompt("Name 2","Jhon Pollak" );
d.prompt("Weight 2",85);
d.prompt("Volume of weist 2",98);
d.prompt("Finished?","No");
d.println(" WeightControl");
d.println(" ======================= ");
for (;!done;) {
indexS1 = Stive.weight;
indexJ1 = Jhon.weight;
indexS1_v = Stive.volume;
indexJ1_v = Jhon.volume;
d.ready("\nPress ready when peoples entered");
int n = d.getInt("Week");
Stive.name = d.getString("Name 1"); //
Stive.weight = d.getDouble("Weight 1"); //
Stive.volume = d.getDouble("Volume of weist 1"); //
Jhon.name = d.getString("Name 2"); //
Jhon.weight = d.getDouble("Weight 2"); //
Jhon.volume = d.getDouble("Volume of weist 2"); //
d.println(Stive.name + ": " + "Weight = " + num.format(Stive.weight) + " Volume of weist = "
+ num.format(Stive.volume) );
d.println(Jhon.name + ": " + "Weight = " + num.format(Jhon.weight) + " Volume of weist = "
+ num.format(Jhon.volume) );
d.println(n + " week ");
if(indexS1 - Stive.weight < indexJ1 - Jhon.weight){
d.println("Jhon result-weight is best: ");
}
else{
d.println("Stive result-weight is best: ");
}
d.println("\tStive is " + num.format(indexS1 - Stive.weight) + " kg throw off" );
d.println("\tJhon is " + num.format(indexJ1 - Jhon.weight) + " kg throw off ");
d.println(" ");
if(indexS1_v - Stive.volume < indexJ1_v - Jhon.volume){
d.println("Jhon result-volume is best: ");
}
else{
d.println("Stive result-volume is best: ");
}
d.println("\tStive is " + num.format(indexS1_v - Stive.volume) + " cm throw off" );
d.println("\tJhon is " + num.format(indexJ1_v - Jhon.volume) + " cm throw off ");
status = d.getString("Finished?");
done = status.equals("Yes");
}//end for
d.println(" ");
d.println("All kg throw off: " );
d.println(" ");
d.println("Stive is " + num.format(indexS - Stive.weight) + " kg throw off" );
d.println("Jhon is " + num.format(indexJ - Jhon.weight) + " kg throw off ");
d.println(" ");
d.println("All cm throw off: " );
d.println(" ");
d.println("Stive is " + num.format(indexS_v - Stive.volume) + " cm throw off" );
d.println("Jhon is " + num.format(indexJ_v - Jhon.volume) + " cm throw off ");
}
public static void main (String [ ] args) {
new WeightControl();
}
}
class Man {
// Declare variables related to a curio
NumberFormat num = NumberFormat.getNumberInstance();
String name;
double weight;
double volume; // volume of weist
// The constructor
Man (String n, double w, double v ) {
name = n;
weight = w;
volume = v;
}
// a method to output the values of the object's variables
void write () {
System.out.println(" Mister " + name + ": " + "Weight = " + num.format(weight) +
" Volume of weist = "
+ num.format(volume) );
}
}
Результат :
Назад |
Начало урока |
Вверх |
Вперед
Содержание