What's new

Closed Pa help naman po Java Language

Status
Not open for further replies.

Behelit

Honorary Poster
Established
Joined
Aug 5, 2017
Posts
420
Reaction
98
Points
233
patingin naman po ng code mga sir/ma'am, di po kasi nagtuturo prof namin ngayong online class, Naghahanap po ako sa internet pero di ko po gets. Pa help naman po matapos tong code salamat po! iintindihin ko nalang po since mas madali ata dito matuto pag may pro coder. 🙏🏻❤️
 

Attachments

https://javatutoring🙏com/java-program-to-calculate-discount/ try mo dito TS. Baka makatulong
 
This is really simple but I don't want to spoon feed the solution. I'll give you hints and questions instead.

1. Do you know how to print a string to the terminal?
2. Do you know how to create a variable?
3. Do you know basic arithmetic? What's 1 + 1? What about 500 * 80%?
4. Do you know how to compute the discount using pen and paper only?
5. Do you know how to create a simple Class in Java? A simple static method?
6. Do you understand in your own words what the business problem is? This is English comprehension.

If the answer is no to any of these, go learn those first before tackling this programming problem.

If the answer is yes to all of these, here's what you should do. Divide and conquer the problem into smaller functions, simple classes or static methids. Whichever is easier for you.

  • Create a function that only does the discount computation.
  • Create a function that just accepts the input
  • Create a function that validates input
  • Create a function that prints the output.

Yes you can write these as one giant function but this is often what leads to confusion among beginners. They write poorly organized code which leads to bugs and harder to understand codes.
 
Also if you search hard enough, there's already similar programs in this forum doing some form of what the problem is asking. You really just have to massage the program.

Also I doubt that you've searched hard enough online via Google. I bet you I can find similar solutions if I do the search. Knowing how to search is an important skill. You don't need to be a programmer to become good at searching by the way.
 
Google mo to:
1. How to enter value in Java.
2. Condition and Loop in Java
3. Basic Arithmetic.
tapos iFrankenstein mo nalang HAHAHAHA

same concept pero coded in python,
total = []
#List sa python, Array tawag sa java.

for i in range(3):
#code will loop 3x
a = int(input('Enter price of item {}: '.format(i+1)))
total.append(a)
#total.append(a) would pass the value of var a to var total

print('Total is {}'.format(sum(total)))
if sum(total) < 500:
print('No discount')
d = 0
elif sum(total) < 800:
print('20% discount')
d = 0.20
elif sum(total) < 1000:
print('30% discount')
d = 0.30
else:
print('50% discount')
d = 0.50

print('Overall: {}'.format(abs(sum(total)*d - sum(total))))

pede mo lagyan ng exceptions para hindi tatanggap ng characters only int.
Try-Except sa python, di ko alam sa Java.
 
Papractice lods beginner palang po
Java:
Scanner sc = new Scanner(System.in);
   System.out.println("Enter amount of items ");
  
   System.out.println("item 1: ");
   int item1 = sc.nextInt();
   System.out.println("item 2: ");
   int item2 = sc.nextInt();
   System.out.println("item 3: ");
   int item3 = sc.nextInt();
  
   double total = item1 + item2 + item3;
   System.out.println("Total purchase: "+ total);
  
   double percent20 = total*0.20;
   double percent30 = total*0.30;
   double percent50 = total*0.50;
  
   if (total < 500){
     System.out.println("No discount");
    
   }else if (total <= 700){
  
   percent20 = total-percent20;
     System.out.println("Discounted amount: "  + percent20);
    
   } else if (total <= 1000){
  
   percent30 = total-percent30;
     System.out.println("Discounted amount: " + percent30);
    
   }else if (total > 1000){
    percent50 = total-percent50;
     System.out.println("Discounted amount: " + percent50);
   }
  
  }
}
 
[XX='KilleerR8, c: 429030, m: 1110355'][/XX]
Java:
import java.util.Scanner;
public class Main {
 
 static Scanner input = new Scanner(System.in);
 
  public static void main(String[] args) {
      
     double item1 = itemValue(1);
     double item2 = itemValue(2);
     double item3 = itemValue(3);
    
     double total = item1 + item2 + item3;
    
     System.out.println("---------------");
    
     if(total < 500){
       System.out.println("Total: "+total);
       System.out.println("No Discount");
     }else{
       System.out.print("Discounted Amount: ");
       System.out.println(total - discount(total));
     }
    
    
  }
 
  public static double discount(double price){
    if(price < 500) return 0;
    if(price <= 799) return 0.20 * price;
    if(price <= 1000) return 0.30 * price;
    return  0.50 * price ;
  }
 
  public static double setItem(int itemNumber){
    System.out.print("Item ");
    System.out.print(itemNumber);
    System.out.print(": ");
    return input.nextDouble();
  }
 
  public static double itemValue(int itemNumber){
    double item = setItem(itemNumber);
      while(item < 0){
         System.out.println("Invalid Please try Again!");
        item = setItem(itemNumber);
      }
      return item;
  }
 
 
 
 
 
}
 
pwedi din ganito.
[XX='Gown Ainz, c: 429144, m: 1780582'][/XX]
Java:
import java.util.Scanner;

public class Remote {
 
 static Scanner input = new Scanner(System.in);
 
  public static void main(String[] args) {
      
     double [] items = new double [3];
    
     double total = 0;
    
     for(int i = 0; i<items.length;i++){
       System.out.print("Enter item ");
       System.out.print((i + 1) + ": ");
       double temp = input.nextDouble();
        if(temp < 0){
          System.out.println("Invalid try again");
          i -= 1;
          continue;
        }
       items[i] = temp;
       total += items[i];
     }
    
     System.out.println("---------------");
    
     if(total < 500){
       System.out.println("Total: "+total);
       System.out.println("No Discount");
     }else{
       System.out.print("Discounted Amount: ");
       System.out.println(total - discount(total));
     }
    
    
  }
 
  public static double discount(double price){
    if(price < 500) return 0;
    if(price <= 799) return 0.20 * price;
    if(price <= 1000) return 0.30 * price;
    return  0.50 * price ;
  }
 
}
 
Status
Not open for further replies.

Similar threads

Back
Top