Tugas Array dan Inheritance Kelompok 03


KELOMPOK 3 :

IKHSAN HERMAWAN

YOGA SAKTI PRATAMA

DAFFA NAUFAL M

Soal Halaman 557

  1. Write a program in a class CountPoor that counts the number of families that are considered poor. Write and use a class Family that has the attributes

· income – a double value that is the income for the family

· size – the number of people in family

And the following methods :

· Family(income, size)-a constructor that sets the attributes

· isPoor(housingCost, foodCost)-a method that returns true if housingCost + foodCost * size is greater than half the family income (foodCost is the average food cost for an individual, while housingCost is for the family)

· toString-a method that returns a string containing the information about the family

The program should read an integer k from the keyboard and then create a array of size k whose base type is Family. It should then create k objects of type Family and put them in the array, reading the income and size for each family from the keyboard. After reading an average housing cost and average food cost from the keyboard it should display the families that are poor.

Penjelasan soal : Soal ini menyuruh kita untuk menentukan keluarga mana yang termasuk keluarga miskin, dengan memasukkan income, housingcost, dan foodcost. Kita disuruh menggunakan 2 class, yang pertama class bernama CountPoor yang berisi script untuk menentukan keluarga mana yang tergolong Poor, dan class kedua bernama Family yang beisikan method-method yang akan digunakan pada main class.

Script Code Java :

Class CountPoor

  1. package countpoor;
  2. import java.util.Scanner;
  3. /**
  4. *
  5. * @author X550VX i7
  6. */
  7. public class CountPoor {
  8. /**
  9. * @param args the command line arguments
  10. */
  11. public static void main(String[] args) {
  12. int k; // total families
  13. int poorCount = 0; // poor families
  14. double familyIncome;
  15. int familySize;
  16. double averageHousingCost;
  17. double averageFoodCost;
  18. Scanner input = new Scanner(System.in);
  19. System.out.print(“Masukkan jumlah keluarga : “);
  20. k = input.nextInt();
  21. Family[] families = new Family[k];
  22. Family[] poorFamilies = new Family[k];
  23. for (int family = 0; family < k; family++) {
  24. System.out.print(“\nJumlah pemasukkan dalam keluarga ” + (family + 1) + “: Rp”);
  25. familyIncome = input.nextDouble();
  26. System.out.print(“Masukkan jumlah anggota keluarga ” + (family + 1) + “: “);
  27. familySize = input.nextInt();
  28. families[family] = new Family(familyIncome, familySize);
  29. System.out.print(“Masukkan rata-rata biaya perbaikan rumah pada keluarga ” + (family + 1) + “: Rp”);
  30. averageHousingCost = input.nextDouble();
  31. System.out.print(“Masukkan rata-rata biaya konsumsi pada keluarga ” + (family + 1) + “: Rp”);
  32. averageFoodCost = input.nextDouble();
  33. if (families[family].isPoor(averageHousingCost * familySize, averageFoodCost)) {
  34. poorFamilies[family] = families[family];
  35. poorCount++;
  36. }
  37. }
  38. System.out.println(“\nTotal jumlah keluarga yang dimasukkan : ” + k);
  39. System.out.println(“Jumlah keluarga yang kurang mampu : ” + poorCount);
  40. System.out.println(“Keluarga yang kurang mampu adalah…”);
  41. for (int family = 0; family < k; family++) {
  42. if (poorFamilies[family] != null) {
  43. System.out.println(“Keluarga ” + (family + 1) + “: ” + poorFamilies[family].toString());
  44. }
  45. }
  46. }
  47. }

Class Family :

  1. package countpoor;
  2. /**
  3. *
  4. * @author X550VX i7
  5. */
  6. public class Family {
  7. private double income;
  8. private int size;
  9. public Family(double newIncome, int newSize) {
  10. income = newIncome;
  11. size = newSize;
  12. }
  13. public boolean isPoor(double housingCost, double foodCost) {
  14. return ((housingCost + foodCost * size) > (income / 2));
  15. }
  16. public String toString() {
  17. return “Income sebesar Rp” + income + “, Anggota keluarga ada ” + size;
  18. }
  19. }

Hasil Output :


Leave a Reply