This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2015/12/22

what is wrong with this

divinity divinity

2015/12/22

#
hi I am doing a proramming assignment which is due on wednesday 23 december midnigh i need the help really bad ans urgently. it is a program where you have to develop a point of sales system program for a company the calculation is: for each item it must capture an item number, item description, unit price and quantity. the program must calculation the extended price for each item. the extended price of an item is multiple by the unit price by the quantity purchased. the program must calculated the customer grand total. the grand total is calculated by totaling or summing the extended price. and must generate the customer final price. it is done by subtracting the grand total from the discount amount. at the end the program must prompt the cashier for the amount of cash given by the customer and it must generate the customer change( customer cash given - final price. this program must implement 3 method called calgrandtotal(which accept the appropriate argument) calDiscountAmount(arguement) and calFinalPrice(appropriate arguement). and it must also store in arrays the item numbers, item descriptions, unit prices, quantites and extended price. I am total confused as to how to get this program to work in the proper way. here is my codes
  public static double calGrandTotal(double extendedprice[])
    {
       int  i;
        double sum = 0;
        
        
        for(i = 1; i<=5; i++)
        {
            sum += extendedprice[i];
                   
        }
        
        return sum;
    }
     public static double calDiscountAmount(double calGrandTotal,  double discountamt) 
    {
         double  total=0;
         
         total = (calGrandTotal * discountamt);
          
         return discountamt;
    }
     public static double calFinalPrice(double calGrandTotal,double discountamt)
     {
         double finalprice = 0;
         
         finalprice = (calGrandTotal - discountamt);
         
        
         return finalprice;
    }

    public static void main(String[] args) 
    {//start main
        Scanner userinput = new Scanner(System.in);
        
        double   extendedprice,  cashgiven=0, finalprice = 0, custchge = 0, gtotal = 0;
        int i = 0;

        
        
        
        int item_num[] = new int[10];
        double item_descript[] = new double[5];
        double unit_price[] = new double[5];
        double quantities[] = new double[5];
        double extendedprices[] = new double[5];
        
        
                
        System.out.println("Hello can you tell me your name please");
        String firstname = userinput.next();
        
        System.out.println("Hello can you tell me your last name please");
        String lastname = userinput.next();
        
        System.out.println("Hello can you tell me what is the amount of item you want to purchase");
        double amt_of_items = userinput.nextDouble();
        
        System.out.println("Hello what was the discount amount on the bill");
        double disc_amt = userinput.nextDouble();
        
        System.out.println("Hello can you tell me what is the item number");
        item_num[i] = userinput.nextInt();
        
        for(i=1; item_num[i]<=5;i++)
        {
                extendedprice = (unit_price[i] * quantities[i]);
                
                calGrandTotal(extendedprice);
                calDiscountAmount(GrandTotal(extendedprice), discountamt);
                calFinalPrice(GrandTotal, discountamt);  
                
        }
        for(i=1; item_num[i]<=5; i++)
        {       
                System.out.println("the customer name is "+firstname + lastname);
                System.out.println("the change given is "+ custchge);
                System.out.println("the discount amount is "+ disc_amt);
                System.out.println("the grand total is "+ gtotal);
                System.out.println("the final price is "+ finalprice);
        }
        
        
        do i have to used two for loops in this program. and can someone explain what is wrong with the calculatio, if  i put the correct arguement in the method and if i done the correct way to do the arrays.
    }//end main  
danpost danpost

2015/12/22

#
Well, for one thing, your 'calDiscountAmount' method is returning the given value 'discountamt' (the 'total' value is lost).
divinity divinity

2015/12/22

#
so am supposed to return the grandtotal and i am getting a set of error when am calling the method in the main
danpost danpost

2015/12/22

#
1) You have an array for ten item numbers, yet your other arrays (for description, unit price, etc) can only hold five values. Why? 2) Line 64 attempts to use 'i' for the index in the 'item_num' array; yet, it is not initialized anywhere (it is not within any loop, either). 3) Your 'for' loops (lines 66 and 75) are using:
unit_num[i]<=5
for the condition to break loop. Why would you use an item number there? 4) Line 70 calls a method to calculate something and have the result returned; yet, that returned value does not go anywhere (it is not used inside a method or an external calculation and it is not being saved to a variable). Lines 71 and 72 have the same issue. 5) I would think that lines 77 to 81 would be executed once per run of the program. Why are those lines within a 'for' loop?
divinity divinity

2015/12/22

#
i thought that a for loop should be used because the question was stressing for each item so i thought a for loop was to be used boi am i worng
divinity divinity

2015/12/24

#
hi danpost i just want to say thank to i finally got the program to figure. i have another game program to figure for me , it have a lot of logical error that I can't seem to see but I know it is there because it is not running the way it supposed to do. as soon as I get a reply from u, i will post it. I have to hand this up on the 31 december 2015
danpost danpost

2015/12/24

#
divinity wrote...
as soon as I get a reply from u, i will post it. I have to hand this up on the 31 december 2015
Please consider this a reply.
divinity divinity

2015/12/24

#
okay, as i said before in my last post, it is a multiple choice that i am building. the game goes like this. it has 3 round, first round-add, second round-subtract and third round-divide, first round has 4 levels, second round has 4 round and third round has two levels, so it is ten question in all the game supposed to have. i have finished coding it, so now am running to see if there is any errors, no syntax errors but logical. the game supposed to run like, it would generated question for the used to answer. a right and wrong answer, if during the course the of the game, the user enter the wrong answer, supposed to iterate back to the same question until the user entered the correct answer. and it is not supposed to go the next level until the correct answer is entered. my game is not doing just as i explained above. it goes straight to the next question until it reach round two and it ask one question in round two and for some reason it jump back to round one . would it be possible if the logical can be pointed out to me, here is the codes that I have any help would be greatly appreciated. used
 
        System.out.println("******************welcome to round 1************************");
        
        System.out.println("Hi what is your name");
        playername = userinput.next();
        System.out.println(playername+" you have a very unique name, my dearest");
        System.out.println(playername+" how old are you");
        playerage = userinput.nextInt();
        //initializing the player name and age
        
         // this is checking to see wjther the player entered the correct age
        if(playerage < 8 || playerage > 13)
        {
            System.out.println(playername+" you have entered an invalid age");
            System.exit(0);//this indicating that the player has entered the wrong age, so the game exit
        }
        else
        {
            System.out.println(playername+" come let's play");
            //this is statinf that the player entered the correct age
        }
        
         i =1;
         while(i<4)
         {
             if(i<=4)
             {
                 //get random numbers
                 rand = 1 + randobj.nextInt(25);//numbers to be generated
                 rand2 = 1 + randobj.nextInt(8);//to hold the second set of random numbers
                 correctans = rand + rand2;//indicating what is to be adding
                 incorrectans = 25 + randobj.nextInt(50);//to hold all the incorrect numbers
                 
                 //letting the user knw what round and level they will be starting at
                 System.out.println(playername+" round 1 "+ " level "+ i + " question " + i + " you are required to match the letter with the corresponding answer");
                 System.out.println(playername+" please add " + rand+ " + " + rand2);
                 
                 //get multiple choice answers
                 System.out.println("A:"+correctans);
                 System.out.println("B:"+incorrectans);
                 choice = userinput.next().charAt(0);
                 
                    if(choice == 'A' || choice == 'a')
                    {
                        System.out.println(playername+" aaaawwwsome, correct is right, you are doing great");
                        score += score + 650;
                        System.out.println("round 1");
                        System.out.println("level "+ i);
                        totalscore += score;
                        System.out.println("the total score is "+ totalscore);
                        i++;
                        correctans++;
                        
                    }
                    else if(choice == 'B' || choice == 'b')//this is indicating that any character can be entered
                    {
                        System.out.println(playername+" come on now, that is incorrect, please try again if you must");
                        score -= score - 225;
                        System.out.println("round 1");
                        System.out.println("level "+ i);
                        totalscore -= score;
                        System.out.println("the total score is "+ totalscore);
                        i++;
                        wrongcount++;
                    }
                    
             }
         }              
                     if(wrongcount < 4)
                     {
                         System.out.println(playername+" round 1 "+ " level "+ i + " question " + i + " you are required to match the letter with the corresponding answer");
                         System.out.println(playername+" please add " + rand+ " + " + rand2);

                        //get multiple choice answers
                        System.out.println("A:"+correctans);
                        System.out.println("B:"+incorrectans);
                        choice = userinput.next().charAt(0);
                 
                    if(choice == 'A' || choice == 'a')
                    {
                        System.out.println(playername+" aaaawwwsome, correct is right, you are doing great");
                        score += score + 650;
                        System.out.println("round 1");
                        System.out.println("level "+ i);
                        totalscore += score;
                        System.out.println("the total score is "+ totalscore);
                        i++;//to iterate when the wrong answer is entered to be given another chance to answer correctly
                        
                    }
                    else if(choice == 'B' || choice == 'b')//this is indicating that any character can be entered
                    {
                        System.out.println(playername+" come on now, that is incorrect, please try again if you must");
                        score -= score - 225;
                        System.out.println("round 1");
                        System.out.println("level "+ i);
                        totalscore -= score;
                        System.out.println("the total score is "+ totalscore);
                        wrongcount++;//to hold all the incorrect ans
                    }
                    
                    }
                     else if(wrongcount == 4)
                     {
                         System.out.println(playername+" too many invalid tries");//indicating that their chances have all used up
                     }
                  
                     
                    
                     
                     System.out.println("*****************welcome to round 2**********************");
                     
                     
                     i=1;
                     while(i<4)
                     {
                         if(i<=4)
                         {
                             //get random numbers
                             num1 = 1 + randobj.nextInt(25);
                             num2 = 2 + randobj.nextInt(8);
                             correctans = num1 - num2;
                             incorrectans = 25 + randobj.nextInt(50);
                             
                             for(i=1; i<=4; i++)
                             {
                                 if(i==1)
                                 {
                                     smallest = num2;
                                     largest = num1;
                                             
                                 }
                                 if(num1 < smallest)
                                 {
                                     smallest = num2;
                                 }
                                 if(num1> largest)
                                 {
                                     largest = num1;
                                 }
                             }
                         }
                     }
                       System.out.println(playername+" round 2 "+ " level "+ i + "question "+ i+" you are required to match the letter with the correct answer");
                       System.out.println(playername+" the max_num and min_num entered is: please subtract "+ largest+" - "+ smallest);
                       
                       System.out.println("A:"+ incorrectans);
                       System.out.println("B:"+correctans);
                       choice = userinput.next().charAt(0);
                       
                       if(choice == 'A'|| choice == 'a')
                       {
                           System.out.println(playername+" oh bummer!!!! you are so wrong, that is not correct");
                           score -= score - 225;
                           System.out.println("round 2");
                           System.out.println("level "+ i);
                           totalscore -= score;
                           System.out.println("the total score is "+ totalscore);
                           i++;
                       }
                       else if(choice == 'B' || choice == 'b')
                       {
                           System.out.println(playername+" well done kiddo, keep up the good work, it is correct");
                           score += score + 650;
                           System.out.println("round 2");
                           System.out.println("level "+i);
                           totalscore += score;
                           System.out.println("the total score is "+ totalscore);
                           correctans++;
                       }
                       
                       if(wrongcount < 4)
                       {
                            System.out.println(playername+" round 1 "+ " level "+ i + " question " + i + " you are required to match the letter with the corresponding answer");
                            System.out.println(playername+" please add " + rand+ " + " + rand2);

                           //get multiple choice answers
                           System.out.println("A:"+correctans);
                           System.out.println("B:"+incorrectans);
                           choice = userinput.next().charAt(0);
                 
                         if(choice == 'A' || choice == 'a')
                        {
                            System.out.println(playername+" aaaawwwsome, correct is right, you are doing great");
                            score += score + 650;
                            System.out.println("round 2");
                            System.out.println("level "+ i);
                            totalscore += score;
                            System.out.println("the total score is "+ totalscore);
                            i++;
                        
                        }
                        else if(choice == 'B' || choice == 'b')//this is indicating that any character can be entered
                        {
                            System.out.println(playername+" come on now, that is incorrect, please try again if you must");
                            score -= score - 225;
                            System.out.println("round 2");
                            System.out.println("level "+ i);
                            totalscore -= score;
                            System.out.println("the total score is "+ totalscore);
                            wrongcount++;
                         }
                    
                         }
                         else if(wrongcount == 4)
                         {
                            System.out.println(playername+" too many invalid tries");
                         }
                       
                       
                       System.out.println("*************welcome to round 3 *************************************");
                       
                       
                        i=1;
                        while(i<2)
                        {
                            if(i<=2)
                            {
                                numerator   = 1 + randobj.nextInt(25);//holding the first generate required numbers
                                denominator = 2 + randobj.nextInt(8);
                                num1 = 2 + randobj.nextInt(20);
                                num2 = 2 + randobj.nextInt(25);
                                correctans = numerator = denominator * num2;
                                correctans = numerator / denominator;
                                incorrectans = 25 + randobj.nextInt(50);
                                
                                  if(numerator%2!=denominator)
                                  {
                                      //letting the players know of the round and level they are in
                                      System.out.println(playername+" round 3 "+" level "+ i +" question "+ i + " you are rquired to match the letter with the correct answer");
                                      System.out.println(playername+" please divide "+ numerator+ " / "+ denominator);
                                      
                                      //getting the multiple choice answers
                                      System.out.println("A:"+correctans);
                                      System.out.println("B:"+incorrectans);
                                      choice = userinput.next().charAt(0);
                                      
                                         if(choice == 'A' || choice == 'a')
                                         {
                                             System.out.println(playername+" okay now, we getting somewhere, well done, that is so correct");
                                             score += score + 650;
                                             System.out.println("round 3");
                                             System.out.println("level "+ i);
                                             totalscore += score;
                                             System.out.println("the totals score is "+ totalscore);
                                             correctans++;
                                         }
                                         else if(choice == 'B' || choice == 'b')
                                         {
                                             System.out.println("damn!!!! not again, that is incorrect");
                                             score -= score -225;
                                             System.out.println("round 3");
                                             System.out.println("level "+ i);
                                             totalscore -= score;
                                             System.out.println("the total score is "+ totalscore);
                                             i++;
                                             
                                         }
                                  }
                            }
                        } 
                                            
                                            
                                            if(wrongcount < 4)
                                            {
                                                System.out.println(playername+" round 1 "+ " level "+ i + " question " + i + " you are required to match the letter with the corresponding answer");
                                                System.out.println(playername+" please add " + rand+ " + " + rand2);

                                               //get multiple choice answers
                                               System.out.println("A:"+correctans);
                                               System.out.println("B:"+incorrectans);
                                               choice = userinput.next().charAt(0);

                                            if(choice == 'A' || choice == 'a')
                                            {
                                                System.out.println(playername+" aaaawwwsome, correct is right, you are doing great");
                                                score += score + 650;
                                                System.out.println("round 3");
                                                System.out.println("level "+ i);
                                                totalscore += score;
                                                System.out.println("the total score is "+ totalscore);
                                                i++;

                                            }
                                            else if(choice == 'B' || choice == 'b')//this is indicating that any character can be entered
                                            {
                                                System.out.println(playername+" come on now, that is incorrect, please try again if you must");
                                                score -= score - 225;
                                                System.out.println("round 3");
                                                System.out.println("level "+ i);
                                                totalscore -= score;
                                                System.out.println("the total score is "+ totalscore);
                                                wrongcount++;
                                            }

                                            }
                                            else if(wrongcount == 4)
                                            {
                                                System.out.println(playername+" too many invalid tries");
                                            }
                                            
                                            
                                            
                                            if(correctans > totalscore)
                                            {
                                                System.out.println(playername+" you won");
                                            }
                                            else if(correctans < totalscore)
                                            {
                                                System.out.println(playername+" you lose");
                                            }
                                            else if(correctans == totalscore)
                                            {
                                                System.out.println(playername+" game draw");
                                            }
                                            System.out.println("the total score is "+ totalscore);
                                            
                                            System.out.println("the player name is "+ playername);
                                            System.out.println("the player age is "+ playerage);
                                            System.out.println("the question completed is "+ question);
                                            
divinity divinity

2015/12/24

#
hi danpost i have corrected the part where I said it was jumping to round again when it should be subtracting. here are the problems that i am experiencing in round 1- supposed to have four question which is does but when the user entered a wrong answer, say like on level 3- it supposed to generate the same question and it supposed to stay on level 3 until the user get the answer correctly and then you move on to the next level and it would be the same for each level. meaning on each level, if any question is answered incorrectly, it supposed to repeat the same question over and over until it entered correctly and then move on to the next level. round 2- it is only generated two question which in general is should generate four question as in round one and the same rules that is applied in round is applied for both round 2 and 3. in round 2 as ah say, it generating only two question and it is iteration but when it iterated it generate the wrong answer when it should a wrong and right answer which should be the same answer in the previous question. but instead is giving me the same question but wrong answer. round 2 should saying for each level like, round 2, level 5, 6, 7 and 8 but am not getting to do dah at all round 3 - is looping on level one when it should level 9 question 9 and the last one should say level 10 question 10 but instead it is in a infinite loop can u help with these logicial errors and how to fix them
danpost danpost

2015/12/24

#
Okay -- so 'i' is controlling the question number. Let the outside while condition break at 'i == 10'. You can use the value of 'i/4' to determine the operation to perform (or, which round the user is currently on) and 'i%4' to determine the question number within that round (use 'if' during the set-up of the question). Use a secondary 'while' inside the main one to keep the question going until a correct answer is given. You could build all the data needed beforehand (before the first 'while' loop), placing the data in arrays to be used during gameplay. This would allow the code to be extensively reduced.
divinity divinity

2015/12/24

#
it is also controlling the level. I am not all that versed in arrays, i know it would help the game since arrays is not my strong point it is difficult for me to understand how to place the data in arrays.
danpost danpost

2015/12/24

#
divinity wrote...
it is also controlling the level. I am not all that versed in arrays, i know it would help the game since arrays is not my strong point it is difficult for me to understand how to place the data in arrays.
Maybe looking over this page of the java tutorials might help.
You need to login to post a reply.