Глава 3
Задача 3.19 "Манускрипт "
Измените программу из примера 2.10 ("Книга") так, чтобы класс записывал количество страниц манускрипта, и добавьте метод для вычисления окончательного количества страниц, принимая во внимание редукцию на 80%. Проверьте класс с помощью программы-теста, которая создает 15 глав со сгенерированным случайным образом количеством страниц от 20 до 50.
Решение
Код программы:
class Book {
int random_01 (int min, int max ){
int z =0;
while(z < min ){
z = (int) (Math.random()* max);
}
return z;
}
// Declare three object variables
Chapter ch1;
String nameChapters[] = {
"Introduction",
"Simple Program",
"Types and Methods",
"Input and Output",
"Manage to stream",
"Massives and Tables",
"Formatting",
"Objects:employing to practic",
"Abstracting and Heritance",
"Graphics and user Interfaces",
"Manage of events",
"Applets in action",
"Work with several routing",
"Work in Net",
"Datastructures and algorithms"
};
// The constructor for the program is
// where the initial work gets done
long s;
Book () {
// Create 15 objects with different initial values
System.out.println();
for (int i = 0;i<15;i++){
ch1 = new Chapter((i+1), nameChapters[i], random_01(20,50));
// Print the values contained in each of the objects
ch1.write();
System.out.println(" Chapter " + ch1.num + " "+ '"' +ch1.name + '"' + " " +ch1.colPages() + " pages as typed in book" );
System.out.println();
s = s + Math.round(ch1.pgs);
}
// Print out a header
System.out.println();
long q = Math.round(s*0.8);
System.out.println("All pages in manuscript " + s );
System.out.println("All pages in typed book " + q );
System.out.println();
}
// All programs must have a main method
public static void main (String [ ] args) {
// Start the program running from its constructor
new Book ();
}
}
class Chapter {
// Declare variables
String name;
long pgs;
int num;
// The constructor
Chapter (int j, String n, int s ) {
name = n;
pgs = s;
num = j;
}
// a method to output the values of the object's variables
void write () {
System.out.println(" Chapter " + num + " "+ '"' + name + '"' + " " + pgs + " pages as manuscripn" );
}
long colPages(){
long n = Math.round(pgs*0.8);
return n;
}
}
Результат :
Chapter 1 "Introduction" 44 pages as manuscripn
Chapter 2 "Simple Program" 31 pages as manuscripn
Chapter 3 "Types and Methods" 24 pages as manuscripn
Chapter 4 "Input and Output" 46 pages as manuscripn
Chapter 5 "Manage to stream" 38 pages as manuscripn
Chapter 6 "Massives and Tables" 38 pages as manuscripn
Chapter 7 "Formatting" 46 pages as manuscripn
Chapter 8 "Objects:employing to practic" 23 pages as manuscripn
Chapter 9 "Abstracting and Heritance" 20 pages as manuscripn
Chapter 10 "Graphics and user Interfaces" 29 pages as manuscripn
Chapter 11 "Manage of events" 42 pages as manuscripn
Chapter 12 "Applets in action" 38 pages as manuscripn
Chapter 13 "Work with several routing" 37 pages as manuscripn
Chapter 14 "Work in Net" 45 pages as manuscripn
Chapter 15 "Datastructures and algorithms" 36 pages as manuscripn
All pages in manuscript 537
Chapter 1 "Introduction" 35 pages as typed in book
Chapter 2 "Simple Program" 25 pages as typed in book
Chapter 3 "Types and Methods" 19 pages as typed in book
Chapter 4 "Input and Output" 37 pages as typed in book
Chapter 5 "Manage to stream" 30 pages as typed in book
Chapter 6 "Massives and Tables" 30 pages as typed in book
Chapter 7 "Formatting" 37 pages as typed in book
Chapter 8 "Objects:employing to practic" 18 pages as typed in book
Chapter 9 "Abstracting and Heritance" 16 pages as typed in book
Chapter 10 "Graphics and user Interfaces" 23 pages as typed in book
Chapter 11 "Manage of events" 34 pages as typed in book
Chapter 12 "Applets in action" 30 pages as typed in book
Chapter 13 "Work with several routing" 30 pages as typed in book
Chapter 14 "Work in Net" 36 pages as typed in book
Chapter 15 "Datastructures and algorithms" 29 pages as typed in book
All pages in typed book 430
Interactive Session Ended
Назад |
Начало урока |
Вверх |
Вперед
Содержание