• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Online Java learning without a human to ask questions of

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am studying Java through Amazon's ATA Instructure site.  While I feel I am understanding the material adequately, when it comes to actually writing code for their exercises, I am having more problems than I expected.  I think I am not correctly understanding the "instructions" for the exercise.  I am hoping that someone would be able to work with me on what the instructions are wanting me to write because I keep getting errors that I can't figure out how to fix.  When reading the instructions, it seems that they reference elements that I am not aware of, such as values in pre-written arrays.  I am stuck and can't move past this one particular exercise on my own.  I'm not looking for "answers" or code, more so guidance as to what is being asked of me...
 
Marshal
Posts: 79242
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Is the teaching any good? Please show us one of their exercises, and your answer. Preferably a small exercise
 
Adrian Davis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For anyone willing to help, these are the instructions for this particular exercise:

For loops
Introduction
You just got hired at your first programming job. You are very excited and want to get to work as soon as possible. Shortly after, your new manager comes in and gives you your first assignment.

A company needs help creating software for their checkout machine. This company works on a model of payment where instead of charging for each item, they charge based on the total weight of the items. Your task is to create the part of the software that calculates the total weight of individual items. For this activity, you will need to be able to use for loops to do repeated actions and declare local variables within the for loop.

Recall the syntax for a for loop

for (initialExpression; testExpression; updateExpression) {
   // body of the loop
}
Step 1 for loops - local variables
For your part, are given an array called weights that contains floats representing the weight of the items. You want to sum up all the weights in the weights array. There is a condition though. If a customer also has a loyalty card (hasLoyaltyCard), all items under 20 oz (0.76 kg) will have 5% taken of its weight or 95% the original weight.

These variables will be available for you to use:

a float array called weights.
a boolean called hasLoyaltyCard.
a float called totalWeight.
Your code will be done within the CheckoutMachine class located at this path: work/src/main/java/com/ata/CheckoutMachine.java

Specifically, your code will be within the calculateWeight method of the code under the // TODO: Step 1 work goes between the two comments and before the other //

Between the those two comments, do the following:

Create a for loop that goes through the whole weights array.
Within the for loop, do the following:
Create a local variable of type float called weight to store the value of weights at a specific index.
Create a conditional that checks whether weight is under 20 and whether hasLoyaltyCard is true and multiple weight by 0.95 if it is.
Add weight to totalWeight the variable.
Once done, run run-workflow step-01 in your terminal to validate your work for step 1.

If all tests for the step are passing you will see BUILD SUCCESSFUL in your terminal window. If tests are failing you will see BUILD FAILED with the failing tests along with their messages in the output above.

You have completed this step when:

run-workflow step-01 passes with "BUILD SUCCESSFUL"
Step 2 of for loops - troubleshooting
You have just finished your program to calculate the total weight of an array of weights from the first activity. You decided to take a break. You go grab a cup of coffee before going back to work. When you get back, you see your cat playing around with your keyboard. You run over quickly and see that your cat has messed up your code. Can you fix what your cat did to your code?

Fix the code in the CheckoutMachineTroubleshoot class: work/src/main/java/com/ata/CheckoutMachineTroubleshoot.java

Hint: In the CheckoutMachineTroubleshoot class, there is a main method that can be run to test out the calculateWeight method. You can update weights and hasLoyaltyCard to include any values you want to test out. Once you have added values you want to test, click on the Run button to test out your values and changes. You will see the text Total weight: follow by the total weight. You can compare that to what you expect the total weight to be based off the code and make changes a necessary.

Once done, run run-workflow step-02 in your terminal to validate your work for step 2.

If all tests for the step are passing you will see BUILD SUCCESSFUL in your terminal window. If tests are failing you will see BUILD FAILED with the failing tests along with their messages in the output above.

You have completed this step when:

run-workflow step-02 passes with "BUILD SUCCESSFUL"
Submitting your work
You can ensure all tests pass before submitting by clicking the Build button. You will see BUILD SUCCESSFUL in your terminal. If tests are failing you will see BUILD FAILED with the failing test output above.

You may also run run-workflow test-all to validate all steps are passing.

Click the Submit button to submit your work and review your grade.
 
Adrian Davis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



The code between the // comments is the only code I wrote in myself, all others are pre-written.  I have no idea what the values are for float[] weights.  How am I supposed to write a code to test the values of weights if I don't know what numbers are in there to compare?
 
Adrian Davis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,

Thank you for the welcome! In response to your question "Is the teaching any good?" I would say the "textbook" material is fine.  The exercises are not so good.  I think I would be having more success if I had someone I could ask questions to or that could explain what the instructions are truly asking.  Which is why I am looking for help.
 
Saloon Keeper
Posts: 10736
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stop a  second. You're trying to answer several conditions at once. Start simpler. Can you loop through the array and print each weight?
 
Adrian Davis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey,

It's not part of the exercise, but I would have to imagine that it is possible to do.  To be fair, I'm not certain HOW I would go about doing that. I know system.out.print for outputting to the screen, but not sure how to use it to print out the contents of an array.
 
Campbell Ritchie
Marshal
Posts: 79242
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That exercise looks vey complicated. It isn't actually complicated at all. But I have misgivings about anybody who tells me to use the float primitive datatype, because of its poor precision. Did they tell you anything about imprecision with floating‑point numbers? If you are able to cope with any imprecision at all, use double arithmetic. What's more, because floating‑point numbers default to being doubles, they are easier to define; you have no need to write the f as in 0f or 12.34e56f.
Did you copy the numbers correctly from the exercise? 20oz isn't 0.76kg. If you use ounces, you might need a slightly awkward bit of arithmetic to convert them to pounds.

Please indent your code correctly as you go; you can cause yourself no end of confusion with incorrectly‑indented code.
I don't know whether you worked it out from Carey's comments, but I am afraid that loop is incorrect.

I am also afraid that you are not being taught object‑oriented programming (=OOP). That is procedural code. Yes, I know those terms are probably unfamiliar to you. But some people (by no means everybody) take a long time to get from procedural programming to OOP. I would have preferred them to ask you to create a ShoppingBasket object with an array of weights as a field, and a method to calculate the price. Much more in line with OOP, which is the paradigm Java┬« was designed for.
 
Carey Brown
Saloon Keeper
Posts: 10736
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

weight is 0 so weight < 20 is the boolean value  true, then throw away the value so this expression does nothing
set boolean variable hasLoyaltyCard to true, the result of the expression will always be true so it will loop forever
multiple weight  which is zero by .95 which is still zero
 
Campbell Ritchie
Marshal
Posts: 79242
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adrian Davis wrote:. . . . I know system.out.print for outputting to the screen, but not sure how to use it to print out the contents of an array.

I recommend you print out each element in the array. Carey made a good suggestion there; it allows you to see what is happening, and that should confirm that your loop is iterating the array correctly.

Because arrays don't override Object#toString(), you will get confused if you simply print the array. The following is probably the simplest way to print an array:-You will probably find System.out.println(...); easier to us than System.out.print(...);.
 
Adrian Davis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbelll,

You're seeing part of my problem.  I have no idea what those "weights" are.  That part of the code was provided.  There is no reference to it anywhere else that I can find.  I'm certain that what I coded was not correct because I can't get this "step" of the exercise to run. I get these errors when I run-workflow step-01:

> Task :compileJava FAILED                                                                                                            
/mnt/vocwork1/ddd_v1_w_q1x_1476685/asn1074865_34/asn1165922_2/work/src/main/java/com/ata/CheckoutMachine.java:16: error: illegal start
of type                                                                                                                              
   for (weight < 20; hasLoyaltyCard = true; weight *= 0.95) {                                                                        
                 ^                                                                                                                  
/mnt/vocwork1/ddd_v1_w_q1x_1476685/asn1074865_34/asn1165922_2/work/src/main/java/com/ata/CheckoutMachine.java:16: error: not a stateme
nt                                                                                                                                    
   for (weight < 20; hasLoyaltyCard = true; weight *= 0.95) {                                                                        
               ^                                                                                                                    
/mnt/vocwork1/ddd_v1_w_q1x_1476685/asn1074865_34/asn1165922_2/work/src/main/java/com/ata/CheckoutMachine.java:16: error: ')' expected
   for (weight < 20; hasLoyaltyCard = true; weight *= 0.95) {                                                                        
                                          ^                                                                                          
/mnt/vocwork1/ddd_v1_w_q1x_1476685/asn1074865_34/asn1165922_2/work/src/main/java/com/ata/CheckoutMachine.java:16: error: ';' expected
   for (weight < 20; hasLoyaltyCard = true; weight *= 0.95) {                                                                        
                                                          ^                                                                          
4 errors
 
Adrian Davis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if this is helpful or not, but this exercise is hosted on the Vocareum website and has a self-contained coding environment in it.  it has one panel one top that the code is displayed in and a bottom panel that acts as a Java compiler.  It also has a panel on the left side that acts as a file manager where the files that I am instructed to open are found.  Lastly all of the instructions that I printed out earlier are displayed in a fourth panel on the right.  It is both convenient to have all on one screen and also frustrating as seems less functional than a true java SDK.  Thus far in the "textbook" it hasn't addressed functionality of the compiler at all.  The commands I know so far are rudimentary at best: run-workflow step-#
        git init
        git add
        git status
        git commit
        git log

I don't remember much about indenting code properly for readability either, this is something I've been wanting to know.  
 
Campbell Ritchie
Marshal
Posts: 79242
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They will probably provide something likeThen they can use that to test your code.
Don't worry about those compiler errors; that loop will have to be changed anyway.

Never use == true or == false. Both are poor style and error‑prone because you can write = by mistake instead of ==.
Don't use b == true.    Always use plain simple b.
Don't use b == false. Always use plain simple !b.
About !b, look here.
 
Adrian Davis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell, could you put your code in the code that I posted earlier to show me where it would go and would it be literally: system.out.println(myArray)?  or system.out.println(weights)?  I put the import on line 2 and attempted to use the system.out.println(myArray) on line 13, after line 12 where the array is defined.  Using run-workflow step-01 did NOT printout any numbers.  I only get the previous errors repeated.  
 
Campbell Ritchie
Marshal
Posts: 79242
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have some suggestions about indentation here. I disagree with some of the other things in that link.
If you have a loop, you can print each component like thisWhen you start getting the right weights in the right order, you know you have got your loop running correctly At this stage that will suffice. Do the additions as the next stage.
 
Carey Brown
Saloon Keeper
Posts: 10736
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Recall the syntax for a for loop

for (initialExpression; testExpression; updateExpression) {
  // body of the loop
}

The assignment has given you the template for a for() loop. You may still need to google "java for loop tutorial". Can you get a loop to  count from zero to nine?

You also are having trouble with the concept of array. You may need to google "java array tutorial".
 
Carey Brown
Saloon Keeper
Posts: 10736
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know the difference between
and
 
Carey Brown
Saloon Keeper
Posts: 10736
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adrian Davis wrote:Carey,

It's not part of the exercise, but I would have to imagine that it is possible to do.  To be fair, I'm not certain HOW I would go about doing that. I know system.out.print for outputting to the screen, but not sure how to use it to print out the contents of an array.


How would you print out one element of the array? How would you use a loop to go through each element?
 
Campbell Ritchie
Marshal
Posts: 79242
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . google "java for loop tutorial". . . . .

Try here. If you find the index, you will find a section about arrays too. Here it is.
 
Adrian Davis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I actually need to know what the numbers are for the Array?  (I kind of think I do...) The way the instructions are written, I was originally assuming that it wasn't needed for me to know.  That I should be able to complete it without knowing.  None of the things you are suggesting I do have been covered in the material I've learned so far.  I am looking over the links you shared and hope that it helps!  Thank you for trying to help me!  
 
Carey Brown
Saloon Keeper
Posts: 10736
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adrian Davis wrote:Do I actually need to know what the numbers are for the Array?

No.
 
Campbell Ritchie
Marshal
Posts: 79242
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adrian Davis wrote:Do I actually need to know what the numbers are . . .

No, as Carey said.
Users expect to be able to send any valid arguments to your method; the compiler will verify that they have the correct type, and nothing else. Knowing you are getting the correct type of argument provides quite a lot of reassurance, however. You can if you wish add special conditions, e.g. length of array must be > 0, no negative values, but if you do that you should make sure your documentation comments make it clear what restrictions you are applying and what will happen if they are breached.

Thank you for trying to help me!  

That's a pleasure
 
Carey Brown
Saloon Keeper
Posts: 10736
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Array must not be null.
An array length == 0 will return a sum of weights of zero
Array must not contain negative values.
 
Campbell Ritchie
Marshal
Posts: 79242
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you made any progress since yesterday?
 
Adrian Davis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not.  I'm not particularly good at figuring out inferred ideas.  I'm sure Carey's comments are actually very helpful, but most of what I'm working with in the exercise is pre-written and not changeable by me.  I had put this down for a while and need to refresh what I'd learned already.  Fortunately, the links you two posted are better at explaining some of the concepts than what I've been given to study.  I'm hoping it will enable me to forge ahead!  I will keep this thread posted with progress.  It is going to be slow going though.  
 
Campbell Ritchie
Marshal
Posts: 79242
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adrian Davis wrote:. . . not changeable by me. . . .

Of course you can change that. Copy that method into a separate XXX.java file and run it from the command line or terminal. Then you can change as much as you like as often as you like and see what happens.

the links you two posted are better at explaining some of the concepts than what I've been given to study. . . .

Good to hear we gave you good links, but I think it is unfortunate that you were given somet5hing not as good to work from.
As Carey hinted, start small. Write a loop to iterate your array, simply printing the individual weights, as a first stage. Once you get that to work, you will know you have the loop working, and you probably won't have to change the loop. You will have to enhance the insides of the loop, but that is a separate problem.
 
Campbell Ritchie
Marshal
Posts: 79242
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I shall let you out of your misery (well, part of it). As far as I am concerned, the basic form of a for loop is,Note:-
  • 1: No semicolon after the ) at the end of line 1.
  • 2: Formatting according to the JavaRanch CodeRanch suggestions. Keep the spacing exactly like that.
  • 3: If you choose something correctly, and don't use i + 1 or i − 1 or similar, that for of loop will always run correctly.
  • 4: Always start from that basic form. If you do need i + 1 or i − 1 or similar, you will have to make minor changes to those numbers.
  • 5: For an array, you can iterate the whole array by writing myArray.length instead of something. Don't try copying the array size into another variable; that isn't necessary.
  • 6: You are here iterating the whole of the array in a read‑only mode in the “forward/normal” direction, so the enhanced for loop in one of the tutorial links I showed you is to be preferred over the classical for loop. But your exercise probably wants a classical for loop. Many people, myself included call the alterrnative form of the loop a for‑each loop.
  • 7: In a vey restricted scope like a loop, you are allowed one‑letter identifiers.
  • We can now say,orTo repeat what Carey said earlier,
  • 1: start by printing the individual weights on the screen.
  • 2: Add all the weights.
  • 3: Reduce each weight by 5% if they have a loyalty card. (Add weights[i] * 0.95f).
  • 4: Add the bit about weighing less than 20 to the 5% reduction.
  • Once you have made a start, you should be able to implement all that lot in ten minutes
     
    Adrian Davis
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Campbell,

    I think I was approaching this exercise from the wrong standpoint.  I'm not used to any of this (frankly) and probably haven't truly gotten myself into a coding mindset.  From you last post, I think I finally understand this exercise.  This is writing a simple program for say, getting shipping costs for an order that gets discounted if the purchaser has a loyalty card.  I reckon (now) that it doesn't truly matter what weights are in the array as only some of them may be utilized. I think what was mostly throwing me off was that it seems like I'm not coding in an input for the user to use a loyalty card (did I miss that in the instructions?)... It feels like this is more of a tallying page that would just add up prior input, apply the discount (if applicable) and give a discounted weight.  I also find it weird that they'd discount the weight and not a cost associated with that weight (maybe in this example it's technically the same thing).  I'll be working on this today and let you know how it goes!
     
    Campbell Ritchie
    Marshal
    Posts: 79242
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Adrian Davis wrote:. . . I reckon (now) that it doesn't truly matter what weights are in the array as only some of them may be utilized. . . . .

    I would have thought you would use all the numbers in the array. It is easier like that. You don't need to filter any numbers out. You are also not veifying the numbers; I think at this stage it is easier to assume you will only be given numbers in a “sensible” ange, but in real‑life code you would always filter out negative weights, etc. In fact, I think if I got a negative number in the array I would declare the array corrupted and refuse to continue processing.

    I'm not coding in an input for the user to use a loyalty card . . . .

    The template you showed us in you first post takes a parameter for the loyalty card, so that information must be passed to your method. Otherwise the program will fail to compile. The javac tool will take care of that problem

    . . .  more of a tallying page . . . add up prior input, apply the discount (if applicable) . . .

    Part of the exercise is to decide when to apply the discount. Do you discount before adding up or after? Carey and I thought before. Part of programming is working out that sot of thing from the instructions.

    On Wednesday, I wrote: The following is probably the simplest way to print an array:-

    And got it wrong It should have read,[/
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic