Глава 3
Задача 3.3 "Контроль веса "
Два друга на протяжении нескольких недель пытались сбросить вес. У первого начальный вес составлял 100 кг, а объем талии - 98 см. Параметры второго - 85 кг и 95 см. Каждую неделю они фиксировали свои параметры и вычисляли, у кого эти показатели уменьшились значительнее.
Решение
Разработаем класс Man для записи необходимых нам данных человека. Для этого в классе будут три переменных - имя, вес, объем талии. В конструкторе эти переменные будут инициализироваться. Ну и как обычно метод write (), который будет сообщать на экран данные каждого экземпляра класса.
class Man {
Поскольку мы используем тип переменных double, то чтобы не выводить много чисел после запятой, используем объект класса NumberFormat, который усекает значения при выводе до трех чисел после запятой.
Создадим два объекта класса Man:
Man Stive = new Man("Stive",100,98);
Man Jhon = new Man("Jhon",85,98);
Stive.write();
Jhon.write();
И далее простые математические вычисления, понятные из условия и кода программы.
Код программы:
import java.text.*;
import javagently.*;
import java.util.*;
import java.io.*;
class ManWeight {
ManWeight () {
NumberFormat num = NumberFormat.getNumberInstance();
Man Stive = new Man("Stive",100,98);
Man Jhon = new Man("Jhon",85,98);
Stive.write();
Jhon.write();
//weight
double k = 100 ;
double m = 98.5;
double k1 = 85.5;
double m1 = 83.5;
double l = 1.5;
double indexS = Stive.weight;
double indexJ = Jhon.weight;
double indexS1;
double indexJ1;
//volume
double k_v = 98 ;
double m_v = 96;
double k1_v = 95;
double m1_v = 93;
double l_v = 2;
double indexS_v = Stive.volume;
double indexJ_v = Jhon.volume;
double indexS1_v;
double indexJ1_v;
for(int i=1;i<7;i++){
System.out.println();
System.out.println(i + " week: ");
indexS1 = Stive.weight;
indexJ1 = Jhon.weight;
Stive.weight = random_02(m,k);
Jhon.weight = random_02(m1,k1);
indexS1_v = Stive.volume;
indexJ1_v = Jhon.volume;
Stive.volume = random_02(m_v,k_v);
Jhon.volume = random_02(m1_v,k1_v);
//weight
k = (k-l);
m = (m-l);
k1 = (k1-l);
m1 = (m1-l);
//volume
k_v = (k_v - l_v);
m_v = (m_v - l_v);
k1_v = (k1_v - l_v);
m1_v = (m1_v - l_v);
Stive.write();
Jhon.write();
if(indexS1 - Stive.weight < indexJ1 - Jhon.weight){
System.out.println("Jhon result-weight is best: ");
}
else{
System.out.println("Stive result-weight is best: ");
}
System.out.println("\tStive is " + num.format(indexS1 - Stive.weight) + " kg throw off" );
System.out.println("\tJhon is " + num.format(indexJ1 - Jhon.weight) + " kg throw off ");
System.out.println();
if(indexS1_v - Stive.volume < indexJ1_v - Jhon.volume){
System.out.println("Jhon result-volume is best: ");
}
else{
System.out.println("Stive result-volume is best: ");
}
System.out.println("\tStive is " + num.format(indexS1_v - Stive.volume) + " cm throw off" );
System.out.println("\tJhon is " + num.format(indexJ1_v - Jhon.volume) + " cm throw off ");
}
System.out.println();
System.out.println("All kg throw off: " );
System.out.println();
System.out.println("Stive is " + num.format(indexS - Stive.weight) + " kg throw off" );
System.out.println("Jhon is " + num.format(indexJ - Jhon.weight) + " kg throw off ");
System.out.println();
System.out.println("All cm throw off: " );
System.out.println();
System.out.println("Stive is " + num.format(indexS_v - Stive.volume) + " cm throw off" );
System.out.println("Jhon is " + num.format(indexJ_v - Jhon.volume) + " cm throw off ");
}
// All programs must have a main method
public static void main (String [ ] args) {
// Start the program running from its constructor
new ManWeight ();
}
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) );
}
}
int random_01 (int min, int max ){
int z =0;
while(z < min ){
z = (int) (Math.random()* max);
}
return z;
}
double random_02 (double min, double max ){
double z =0;
while(z < min ){
z = (Math.random()* max);
}
return z;
}
}
Результат :
1 week:
Jhon result-volume is best:
2 week:
Jhon result-volume is best:
3 week:
Stive result-volume is best:
4 week:
Jhon result-volume is best:
5 week:
Jhon result-volume is best:
6 week:
Stive result-volume is best:
All kg throw off:
Stive is 7,934 kg throw off
All cm throw off:
Stive is 10,214 cm throw off
Mister Stive: Weight = 100 Volume of weist = 98
Mister Jhon: Weight = 85 Volume of weist = 98
Mister Stive: Weight = 99,398 Volume of weist = 96,123
Mister Jhon: Weight = 84,8 Volume of weist = 94,884
Stive result-weight is best:
Stive is 0,602 kg throw off
Jhon is 0,2 kg throw off
Stive is 1,877 cm throw off
Jhon is 3,116 cm throw off
Mister Stive: Weight = 98,051 Volume of weist = 95,287
Mister Jhon: Weight = 83,484 Volume of weist = 91,313
Stive result-weight is best:
Stive is 1,347 kg throw off
Jhon is 1,316 kg throw off
Stive is 0,836 cm throw off
Jhon is 3,571 cm throw off
Mister Stive: Weight = 96,203 Volume of weist = 93,328
Mister Jhon: Weight = 81,446 Volume of weist = 90,14
Jhon result-weight is best:
Stive is 1,848 kg throw off
Jhon is 2,038 kg throw off
Stive is 1,959 cm throw off
Jhon is 1,173 cm throw off
Mister Stive: Weight = 94,531 Volume of weist = 91,406
Mister Jhon: Weight = 79,656 Volume of weist = 88,171
Jhon result-weight is best:
Stive is 1,673 kg throw off
Jhon is 1,79 kg throw off
Stive is 1,922 cm throw off
Jhon is 1,969 cm throw off
Mister Stive: Weight = 93,513 Volume of weist = 88,817
Mister Jhon: Weight = 79,3 Volume of weist = 85,205
Stive result-weight is best:
Stive is 1,018 kg throw off
Jhon is 0,356 kg throw off
Stive is 2,589 cm throw off
Jhon is 2,966 cm throw off
Mister Stive: Weight = 92,066 Volume of weist = 87,786
Mister Jhon: Weight = 77,505 Volume of weist = 84,925
Jhon result-weight is best:
Stive is 1,446 kg throw off
Jhon is 1,795 kg throw off
Stive is 1,031 cm throw off
Jhon is 0,281 cm throw off
Jhon is 7,495 kg throw off
Jhon is 13,075 cm throw off
Назад |
Начало урока |
Вверх |
Вперед
Содержание