PRAKTIKUM 3 Pemrograman Berorientasi Objek

advertisement
PRAKTIKUM 3
Pemrograman Berorientasi Objek
Petunjuk:
-
Cantumkan NAMA dan NRP dalam bentuk comment di setiap source code jawaban
Kumpulkan dalam format: NRP-NAMA-Prak03-{nomor soal yang selesai}.rar, misal:
o Selesai nomor 2 saja, berarti: NRP-NAMA-Prak03-2.rar
o Selesai nomor 2 dan 4 saja, berarti: NRP-NAMA-Prak03-24.rar
o Selesai semua, berarti: NRP-NAMA-Prak03-12345678-rar
Total nilai: 100 + 50 bonus point
Materi:
-
Review Pemrograman dasar Java
Conditional Statement
1.
CheckPassFail.java (5 points)
Write a program called CheckPassFail which prints "PASS" if the int variable "mark" is more than or equal
to 50; or prints "FAIL" otherwise.
public class CheckPassFail { // saved as "CheckPassFail.java"
public static void main(String[] args) {
int mark = 49;
// set the value of mark here!
System.out.println("The mark is " + mark);
if ( ...... ) {
System.out.println( ...... );
} else {
System.out.println( ...... );
}
}
}
2.
CheckOddEven.java (10 points)
Write a program called CheckOddEven which prints "Odd Number" if the int variable “number” is odd, or
“Even Number” otherwise.
Hints: n is an even number if (n % 2) is 0.
public class CheckOddEven {
// saved as "CheckOddEven.java"
public static void main(String[] args) {
int number = 49;
// set the value of number here!
System.out.println("The number is " + number);
if ( ...... ) {
System.out.println( ...... );
} else {
System.out.println( ...... );
}
}
}
3.
PrintNumberInWord.java (15 points)
Write a program called PrintNumberInWord which prints "ONE", "TWO",... , "NINE", "OTHER" if
the int variable "number" is 1, 2,... , 9, or other, respectively. Use (a) a "nested-if" statement; (b) a
"switch-case" statement.
public class PrintNumberInWord {
// saved as "PrintNumberInWord.java"
public static void main(String[] args) {
int number = 5;
// Using nested-if
if (number == 1) {
System.out.println("ONE");
} else if (......) {
......
} else if (......) {
......
......
} else {
......
}
// Using switch-case
switch(number) {
case 1: System.out.println("ONE"); break;
case 2: ......
......
......
default: System.out.println("OTHER");
}
}
}
4. CheckLetter.java (20 points)
Write a program that print "Uppercase", "Lowercase", or "Not a letter" depending on whether the
character stored in ch is an uppercase alphabetic character, a lowercase alphabetic character, or
not an alphabetic character at all.
5. Insurance.java (20 points)
•
•
Write a program to determine the cost of an automobile insurance premium, based on 2
inputs: driver's age and the number of accidents that the driver has had.
RULES/FORMULA:
The basic insurance charge is $500. There is a surcharge of $100 if the driver is under 25 and
an additional surcharge for accidents:
# of accidents Accident Surcharge
1
50
2
125
3
225
4
375
5
575
6 or more
No insurance
6. LeapYear.java (30 points)
Write a program which reads a year (integer) from the user and decides whether that year is a leap
year.
A year is a leap year (and so contains a February 29) if it is divisible by 4.
But if the year is also divisible by 100 then it is not a leap year, unless it is divisible by 400.
This means that years such as 1992, 1996 are leap years because they are divisible by 4 and are not
affected by the rest of the rule which applies to century years such as 1900 and 2000.
Century years are not leap years except where they are a multiple of 400.
Hence, the years 1700, 1800 and 1900 were not leap years and did not contain a February 29.
But the year 2000 was a leap year, the first such century leap year since 1600.
SOAL BONUS
7. ValidDate.java (20 points)
Write a program that reads a date from the user in numeric form.
For example, February 17, 2003 would be entered as the three integers 2, 17, and 2003.
Your program must then determine if the date is a “valid” date.
Use the following information to determine if the date is valid: January, March, May, July, August,
October, and December all have 31 days. April, June, September, and November all have 30 days.
February has 28 days in a non-leap year and 29 days in a leap year.
Echo the input and print either “valid date” or “invalid date” as output.
8. KunciJawaban.java (30 points)
Akhir-akhir ini Nata kerajingan bermain game online hingga subuh. Kebiasaannya tersebut
menyebabkan Nata mengalami masalah dengan pelajaran, Nata sering terlambat bangun, dan sering
tertidur di kelas. Melihat keadaanya, ibunya mengancam akan menjual komputer kesayangan Nata,
sehingga Nata tidak bisa bermain game online lagi.
Walaupun telah mendapatkan peringatak keras dari ibunya, Nata tetap saja tidak bisa menghentikan
kebiasaannya bermain game online, padahal besok ada ulangan matematika. Tiba-tiba saat jam 3
subuh, teman bermain game online Nata menawarkan kunci jawaban untuk ujian matematika Nata.
Melihat kesempatan tersebut, Nata langsung mencatatnya di secarik kertas. (JANGAN DITIRU YA )
Masalahnya sekarang adalah kunci jawaban tersebut ternyata berupa kompresi dari kunci jawaban
sebenarnya. Kompresi yang dipakai sangat sederhana, untuk jawaban yang sama berturut-turut,
maka jawaban hanya ditulis sekali namun diawali oleh angka yang menunjukkan perulangan yang
terjadi. Bantulah Nata memecahkan masalah kompresi tersebut.
Input
Input berupa string S dengan panjang a (0 < a <= 500).yang merupakan kombinasi huruf C dan atau
angka N. Input diakhiri oleh string “END”.
Output
Output berupa string hasil dekompresi dengan aturan setiap karakter C apabila diawali oleh integer
N, maka sewaktu dicetak akan diulangi sebanyak N kali. Output dilakukan untuk setiap baris input.
Sampel
Input : 4a3b2c
12a2b
3abc
END
Output : aaaabbbcc
aaaaaaaaaaaabb
aaabc
Selamat Mengerjakan
© Niko Ibrahim, S.Kom., MIT
Download