Documentation Index
Fetch the complete documentation index at: https://v1-learn.neoartd.my.id/llms.txt
Use this file to discover all available pages before exploring further.
Introduction 1
As part of promotion, a department store provides discounts for its customers.
All products whose prices are above IDR 300,000 are given a discount of 40%. For example, a pair of shoes that cost IDR 499,000 are now sold for only IDR 299,400.
However, the labels printed on the products are still showing prices before the discount, so the department store employee still needs to calculate in advance to get the final price.
Write a program to help department store employees to calculate the final price of the products.
total = price;
if (price > 300000 && date == 7) {
total = total - 0.4 * total; // 40% discount
total = total - 0.2 * total; // additional 20% discount
} else if (price > 300000) {
total = total - 0.4 * total; // 40% discount
} else if (date == 7) {
total = total - 0.2 * total; // additional 20% discount
}
Introduction 2
Because the strategy of providing discounts successfully attracted customers, department store management plans to hold additional events. On the 7th of each month, department stores will provide an additional discount of 20% for all products. For products with prices above IDR 300,000, of course the discounts obtained will double.
For example, for a bag worth IDR 1,000,000, the initial discount obtained is 40% x IDR 1,000,000 = IDR 400,000 so the price of the item before the additional discount = IDR 600,000.
With an additional discount of 20%, the total to be paid is IDR 600,000 - (20% x IDR 600,000) = IDR 480,000.
Update the program you have previously written.
total = price;
if (price > 300000) {
total = total - 0.4 * total;
}
if (date == 7) {
total = total - 0.2 * total;
}
Flag Variables
boolean varFlag = condition;
Sample Case
Sample Solution
Using Flag Variables
Write a program to determine whether the size of the baby at birth is normal
or not
- Normal baby weight at birth ranges from 2.5 to 4.5 kg and lengths between 45 and 60 cm
- Input: baby’s weight in kg and length in cm
- Output: whether the baby’s weight and length are normal, its weight is normal, its length is normal, or both are not normal
if (weight >= 2.5 && weight <= 4.5 && length >= 45 && length <= 60) {
// both weight & length are normal
} else if (weight >= 2.5 && weight <= 4.5) {
// weight is normal, length is not
} else if (length >= 45 && length <= 60) {
// length is normal, weight is not
} else {
// both weight & length are not normal
}
// Flag Variables
boolean weightNormal = (weight >= 2.5 && weight <= 4.5);
boolean lengthNormal = (length >= 45 && length <= 60);
if (weightNormal && lengthNormal) {
// both weight & length are normal
} else if (weightNormal) {
// weight is normal, length is not
} else if (lengthNormal) {
// length is normal, weight is not
} else {
// both weight & length are not normal
}
Nested Ifs
if (condition1) {
// do something
if (condition2) {
// do something
} else {
// do something
}
// do something
}
Sample Case
Negative price allowed?
Solution
Write a program to calculate the total user must pay for an imported item.
- If the price of the item exceeds USD 100, then an import tax of 20% is imposed
- Input: price of items before tax in USD
- Output: total paid (item price plus tax)
Scanner in = new Scanner(System.in);
double price = in.nextDouble();
double total = price;
if (price > 100) {
total = price + 0.2 * price;
}
System.out.println(total);
Scanner in = new Scanner(System.in);
double price = in.nextDouble();
if (price > 0) {
double total = price;
if (price > 100) {
total = price + 0.2 * price;
}
System.out.println(total);
} else {
System.out.println("The price must be positive");
}
Input Validation
Write a program to calculate the volume of the cube
- Input: the length of any edge of the cube
- Output: volume of the cube
Scanner in = new Scanner(System.in);
System.out.print("Enter the length of the cube's edge: ");
double edge = in.nextDouble();
double volume = edge * edge * edge;
System.out.println(volume);
Scanner in = new Scanner(System.in);
System.out.print("Enter the length of the cube's edge: ");
double edge = in.nextDouble();
if (edge > 0) {
double volume = edge * edge * edge;
System.out.println(volume);
} else {
System.out.println("The length of the cube's edge must be positive");
}
Scanner in = new Scanner(System.in);
System.out.print("Enter the length of the cube's edge: ");
if (in.hasNextDouble()) {
double edge = in.nextDouble();
...
} else {
String word = in.next();
System.out.println(word + " is not a number");
}
Scanner in = new Scanner(System.in);
System.out.print("Enter the length of the cube's edge: ");
if (! in.hasNextDouble()) {
String word = in.next();
System.out.println(word + " is not a number");
return; // return keyword
}
double edge = in.nextDouble();
...
String
Strings in Conditionals
Scanner in = new Scanner(System.in);
System.out.print("What is your name: ");
String name = in.next();
if (name == "Ani") {
System.out.println("Your name is Ani");
} else {
System.out.println("Your name is not Ani");
}
String Comparison
.equals() : digunakan untuk membandingkan isi dari dua buah string, dan mengembalikan true jika isi kedua string tersebut sama. string.equals(anotherString)
string.equalsIgnoreCase(anotherString)
string1 = "text";
string2 = "text";
string3 = "Text";
string4 = "txt";
string1.equals(string2); // true
string1.equals(string3); // false
string1.equals(string4); // false
string1.equalsIgnoreCase(string2); // true
string1.equalsIgnoreCase(string3); // true
string1.equalsIgnoreCase(string4); // false
.contains() : digunakan untuk mengecek apakah suatu string mengandung karakter tertentu atau tidak. string.contains(anotherString)
string1 = "once upon a time";
string2 = "time";
string3 = "once upon a time";
string4 = "a book";
string1.contains(string2); // true
string2.contains(string1); // false
string1.contains(string3); // true
string1.contains(string4); // false
.startswith() : digunakan untuk mengecek apakah suatu string diawali dengan karakter tertentu atau tidak.
.endswith() : digunakan untuk mengecek apakah suatu string diakhiri dengan karakter tertentu atau tidak. string.startsWith(anotherString)
string.endsWith(anotherString)
string1 = "once upon a time ";
string2 = "once";
string3 = "time";
string1.startsWith(string2); // true
string1.startsWith(string3); // false
string1.endsWith(string2); // false
string1.endsWith(string3); // true
.compareTo() : digunakan untuk membandingkan dua buah string berdasarkan urutan leksikografisnya.
.compareToIgnoreCase() : digunakan untuk membandingkan dua buah string berdasarkan urutan leksikografisnya tanpa memperdulikan huruf besar atau kecil. string.compareTo(anotherString)
string.compareToIgnoreCase(anotherString)
string1 = "budi";
string2 = "ani";
string3 = "chandra";
string4 = "Budi";
string1.compareTo(string2); // > 0
string1.compareTo(string3); // < 0
string1.compareTo(string1); // 0
string1.compareTo(string4); // > 0
string1.compareToIgnoreCase(string4); // 0
String Manipulation
substring
replaceAll/replaceFirst
toUpperCase/toLowerCase
trim
.substring() : digunakan untuk mengambil sebagian karakter dari suatu string. string.substring(beginIndex)
string.substring(beginIndex, endIndex)
string = "abcdefghij";
string.substring(3); // "defghij"
string.substring(3, 5); // "de"
.replaceAll() : digunakan untuk mengganti semua kemunculan suatu karakter atau string dengan karakter atau string lain.
.replaceFirst() : digunakan untuk mengganti kemunculan pertama suatu karakter atau string dengan karakter atau string lain. string.replaceAll(pattern, replacement)
string.replaceFirst(pattern, replacement)
string = "he's a doctor while she's a nurse";
string.replaceAll("'s", " is");
// "he is a doctor while she is a nurse"
string.replaceFirst("'s", " is");
// "he is a doctor while she's a nurse"
.toUpperCase() : digunakan untuk mengubah semua karakter dalam suatu string menjadi huruf besar.
.toLowerCase() : digunakan untuk mengubah semua karakter dalam suatu string menjadi huruf kecil. string.toUpperCase()
string.toLowerCase()
string = "My name is Ani";
string.toUpperCase();
// "MY NAME IS ANI"
string.toLowerCase();
// "my name is ani"
.trim() : digunakan untuk menghapus spasi di awal dan di akhir suatu string. string.trim()
string =" Ani ";
string.trim();
// "Ani"
String → Number
.parseInt() : digunakan untuk mengubah string menjadi integer.
.parseDouble() : digunakan untuk mengubah string menjadi double.
Integer.parseInt(string)
Double.parseDouble(string)
String time = "18:49";
String h = time.substring(0, 2);
String m = time.substring(3);
int hour1 = Integer.parseInt(h);
int minute1 = Integer.parseInt(m);
double hour2 = Double.parseDouble(h);
double minute2 = Double.parseDouble(m);