The Student Room Group

Newbie Coders Chat

Scroll to see replies



Thanks 571, well the good news is that i got something like 17 out of 22 for that submission, so it was really bang on.

I have had another look at the next one, and done some thinking about it. Also the final deadline for all of the work has been put back til the 17th of this month. You really are a life saver.

Here is the next piece of code which i need help with. We need to change it, so that it takes the string sentance, and string word, and removes the word from the sentance, here is the code so far anyway. See what you think thanks!

John
** @author your_name*/ public class RemoveIt { //TODO: instantiate class variables String sentence; /** @param sentence String to manipulate */ public RemoveIt(String sentence) { //TODO: constructor definition this.sentence = "A kittycat is at the door"; String RemoveIt = sentence } i need to remove the word "cat" from the argument?? /** @param toRemove String to remove from sentence * @return manipulated String */ public String remove(String phrase) { //TODO: remove definition } }
Original post by john2054
Thanks 571, well the good news is that i got something like 17 out of 22 for that submission, so it was really bang on.

I have had another look at the next one, and done some thinking about it. Also the final deadline for all of the work has been put back til the 17th of this month. You really are a life saver.

Here is the next piece of code which i need help with. We need to change it, so that it takes the string sentance, and string word, and removes the word from the sentance, here is the code so far anyway. See what you think thanks!

John
** @author your_name*/ public class RemoveIt { //TODO: instantiate class variables String sentence; /** @param sentence String to manipulate */ public RemoveIt(String sentence) { //TODO: constructor definition this.sentence = "A kittycat is at the door"; String RemoveIt = sentence } i need to remove the word "cat" from the argument?? /** @param toRemove String to remove from sentence * @return manipulated String */ public String remove(String phrase) { //TODO: remove definition } }

I'm glad to hear you got good marks from that submission. I'll work on the rest of the assignments today.
Original post by john2054
Here are the other assignments i have:
A class called GiveChange is provided below. The design of this class is to take a decimal amount from the user representing the change due to them from a purchase, and return the number of quarters, dimes, nickels and pennies that should be given to the user to make that total.The main method has been defined for you, and utilizes four static methods: getQuarters, getDimes, getNickels, and getPennies. Each method takes the total change in pennies (i.e., $3.80 = 380 pennies). As the denomination gets smaller, additional parameters are added to represent the number of quarters, dimes and nickels that minimize the change returned.Write the method definitions for getQuarters, getDimes, getNickels, and getPennies.

/* author your_name*/import java.util.Scanner;public class GiveChange { /** param p number of pennies * return number of quarters */ public static int getQuarters(int p) { //TODO: method definition for getQuarters } /** param p number of pennies * param q number of quarters * return number of dimes */ public static int getDimes(int p, int q) { //TODO: method definition for getDimes } /** param p number of pennies * param q number of quarters * param d number of dimes * return number of nickels */ public static int getNickels(int p, int q, int d) { //TODO: method definition for getNickels } /** param p number of pennies * param q number of quarters * param d number of dimes * param n number of nickels * return number of leftover pennies */ public static int getPennies(int p, int q, int d, int n) { //TODO: method definition for getPennies } public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("How much change is to be given?":wink:; double change = s.nextDouble(); int pennies = (int)(change * 100); int q = getQuarters(pennies); int d = getDimes(pennies, q); int n = getNickels(pennies, q, d); int p = getPennies(pennies, q, d, n); String str = String.format("The customer should recieve %d " + "quarters, %d dimes, %d nickels, " + "and %d pennies.", q, d, n, p); System.out.println(str); }


Solution for GiveChange below:
http://pastebin.com/ctyjriN2

Disclaimer: I suck at math, so my algorithm might not be the most efficient or correct one. Please keep that in mind.

Explanation

Basics (variables) first:

1 penny = 1 cents
1 nickel = 5 cents
1 dime = 10 cents
1 quarter = 25 cents

As the denominator gets smaller, we need more types of coins:

let p = total amount of pennies
q = p / 25;
d = p / 10;
n = p / 5;

Logical steps:

I have to give you 380 cents.

1) How many quarters can I get out of that?
-> Remove 25 pennies until you can't anymore.

2) How many dimes can I get out of the remainder of the above?
-> Remove 10 pennies (if you can) until you cant anymore

3) How many nickels can I get out of the remainder of the above?
-> Remove 5 pennies (if you can) until you cant anymore

4) How many pennies can I get out of the remainder of the above?
-> The pennies that are left.

Mathematical steps:
(example provided with 58 pennies)

a) Get the number of quarters: 58 pennies / 25 = 2 quarters and 0.32 quarters remainder (or 0.32 * 25 = 8 pennies)

b) Now that we know there are 2 quarters in that amount of pennies, we remove that same amount: 50, which leaves us with 8 pennies.

c) Get the number of dimes: 8 pennies / 10 = 0.8 dimes, in other words: 0 dimes.

d) Get the number of nickels: We weren't able to transform those 8 pennies into dimes, so now we are left with them to turn them into nickels.
8 / 5 = 1 nickel, 3 pennies left

e) Get the number of pennies left in total: We just removed 5 pennies from our 8 and now we're left with 3 pennies.
(edited 7 years ago)
Original post by john2054
Thanks 571, well the good news is that i got something like 17 out of 22 for that submission, so it was really bang on.

I have had another look at the next one, and done some thinking about it. Also the final deadline for all of the work has been put back til the 17th of this month. You really are a life saver.

Here is the next piece of code which i need help with. We need to change it, so that it takes the string sentance, and string word, and removes the word from the sentance, here is the code so far anyway. See what you think thanks!

John
** @author your_name*/ public class RemoveIt { //TODO: instantiate class variables String sentence; /** @param sentence String to manipulate */ public RemoveIt(String sentence) { //TODO: constructor definition this.sentence = "A kittycat is at the door"; String RemoveIt = sentence } i need to remove the word "cat" from the argument?? /** @param toRemove String to remove from sentence * @return manipulated String */ public String remove(String phrase) { //TODO: remove definition } }

Solution for RemoveIt below:
http://pastebin.com/AhfqrmjR

Tip: whenever they mention class variable, that means they want you to put the keyword static in front of your variable, like I did in the solution code.

Note: Please post your code in pastebin in the future because TSR doesn't
format it properly.
Original post by 571122
Solution for RemoveIt below:
http://pastebin.com/AhfqrmjR

Tip: whenever they mention class variable, that means they want you to put the keyword static in front of your variable, like I did in the solution code.

Note: Please post your code in pastebin in the future because TSR doesn't
format it properly.


Thanks for helping me with those last programs 571, we got top marks for them!
Original post by john2054
Thanks for helping me with those last programs 571, we got top marks for them!

May I know the specific marks?
Original post by 571122
May I know the specific marks?


100% for both of those programs you just did. The email one wasn't quite full, but i don't know why that was. And I was heavily let down by a multiple choice test i did, where they changed the grading scheme from the best of three, to one chance only. I only got something like 25% for this. But I am confident i will be able to get the 50% pass grade, if you keep on helping me like this.

The final grades aren't released until the end, in two weeks, but i can show you my grades so far. Also i am not sure if i want to carry on to do the second module. Would you still be able to help me as you have been doing, if i do decide to do this? But even still i have a new job starting up in a couple of weeks, and there is also a psychology mooc i have signed up to, so without your help i just won't be able to do it? What do you think 571?

comscigradesv1.jpg
Original post by john2054
Also i am not sure if i want to carry on to do the second module. Would you still be able to help me as you have been doing, if i do decide to do this? But even still i have a new job starting up in a couple of weeks, and there is also a psychology mooc i have signed up to, so without your help i just won't be able to do it? What do you think 571?

Well, John, like you, I have many things to do in my life as well, so here is my suggestion: I'll help you out if I have the time. Until now, the coding has been easy enough to do in a couple of minutes (well, except for the GiveChange one, that took me 24 hours because I suck at math).

The question is, John, if you are learning any valuable lessons from the code I write up. I have the feeling you just accept my solutions and post them, which would not be very beneficial for your learning process.
It's important to learn something new every time and to add that to your knowledge and skill set to be able to do something with it.
Please tell me if you are learning from my code because it's important for now and for your future endeavors

Make sure you read a Java book, you'll help yourself a lot!
(edited 7 years ago)
Original post by 571122
Well, John, like you, I have many things to do in my life as well, so here is my suggestion: I'll help you out if I have the time. Until now, the coding has been easy enough to do in a couple of minutes (well, except for the GiveChange one, that took me 24 hours because I suck at math).

The question is, John, if you are learning any valuable lessons from the code I write up. I have the feeling you just accept my solutions and post them, which would not be very beneficial for your learning process.
It's important to learn something new every time and to add that to your knowledge and skill set to be able to do something with it.
Please tell me if you are learning from my code because it's important for now and for your future endeavors

Make sure you read a Java book, you'll help yourself a lot!


Yes I am learning from it, but maybe not as much as i should. Plus it is kind of like cheating, and i don't want to make a habit of it, so i guess i will call it a day after this one. Thanks for the help anyway!
Original post by john2054
Yes I am learning from it, but maybe not as much as i should. Plus it is kind of like cheating, and i don't want to make a habit of it, so i guess i will call it a day after this one. Thanks for the help anyway!

In school, providing solutions is called 'cheating', in the real world, it's called 'collaboration'. :biggrin:
Original post by 571122
In school, providing solutions is called 'cheating', in the real world, it's called 'collaboration'. :biggrin:


You know you look like eminem?
Original post by john2054
You know you look like eminem?

That's not me, that's Eminem, :biggrin: his name is Marshall Mathers. That image is a screenshot from his video 'Role Model'.
Original post by 571122
That's not me, that's Eminem, :biggrin: his name is Marshall Mathers. That image is a screenshot from his video 'Role Model'.


Yes, I was going to say you must be his twin or something :wink:
I'm stuck and Codecademy's Q&A forum link seems to be broken. Does anybody here know what I did wrong?
Original post by Craghyrax
I'm stuck and Codecademy's Q&A forum link seems to be broken. Does anybody here know what I did wrong?

fix:
1) The console.log() statement needs to be terminated by closing parentheses and a semicolon
2) The "food" string cannot have a quote because it's not a literal string in this case, but a passed-in argument


console.log("I want to eat " + " " + food);


technical explanation:
otherwise the Javascript engine will interpret everything after that line as a statement, which }; is not, which is why the error reads: "invalid or unexpected token" (Javascript divides commands into separate tokens).
Original post by 571122
fix:
1) The console.log() statement needs to be terminated by closing parentheses and a semicolon
2) The "food" string cannot have a quote because it's not a literal string in this case, but a passed-in argument


console.log("I want to eat " + " " + food);


technical explanation:
otherwise the Javascript engine will interpret everything after that line as a statement, which }; is not, which is why the error reads: "invalid or unexpected token" (Javascript divides commands into separate tokens).


PRSOM thanks very much!

I'm having difficulty fully understanding functions in JS. I understand what they are doing, but I don't understand why the syntax is the way that it is. For example:

var orangeCost = function(cost) {
console.log(cost * 5);
};
orangeCost("5");

To my mind, the last line should be: cost("5"); because I thought I told it that the function 'cost' will change, but that the rest of the code in curly brackets should stay the same.... Instead it looks like I told it that the function 'cost' will change and then went ahead and called the variable in the last line, not the function :confused:
Know what I mean?
Original post by Craghyrax

I'm having difficulty fully understanding functions in JS. I understand what they are doing, but I don't understand why the syntax is the way that it is. For example:
var orangeCost = function(cost) {
console.log(cost * 5);
};
orangeCost("5");
To my mind, the last line should be: cost("5":wink:; because I thought I told it that the function 'cost' will change, but that the rest of the code in curly brackets should stay the same.... Instead it looks like I told it that the function 'cost' will change and then went ahead and called the variable in the last line, not the function :confused:
Know what I mean?

Let me see if I properly understand what you are asking.

cost is just the argument that belongs to the named function with name orangeCost. The variable orangeCost now points to an area in memory that represents a function.
At the last line, you are calling the [pre]orangeCost("5":wink:;[/pre] function, passing in the literal string "5" as a parameter because that is a statement (a statement ends with a semicolon). A result of that statement is that the function gets looked up in memory and executed (argument + function body, what's inside the braces) with the parameters that you gave it.
So, cost is a placeholder, that sits there, waiting for a value to be assigned to it. When you call [pre]orangeCost("5":wink:;[/pre] you pass in the value 5 and that will be passed onto cost, so that cost now equals 5.
Finally, it will read: console.log(5*5); which will be 25 as a result.
Using the function name makes the Javascript engine look up that function in memory (RAM). Using the parentheses is a way to pass in parameters, so that if they get used, their values will be redirected and assigned to the function's argument definition. Somewhere in memory there is a place for functions and somewhere in there is a place for the function's arguments too.

I hope this was a decent explanation. If not, feel free to inquiry me further on it.

P.S.: it's not good practice to pass in a string where a number is expected, so "5" should be 5.
P.P.S: I'm not a Javascript expert.

in-depth discussion:

Spoiler

(edited 7 years ago)
Reply 318
Incase you wanted to know even more exciting stuff about JS functions - Javascript has a few different ways of declaring a function.


function orangeCost(arg){
}

orangeCost(5);

Is the most common way. That is called a function declaration. You've declared a function named orangeCost and are calling it passing '5' as the parameter.


var orangeCost = function(cost) {
console.log(cost * 5);
};
orangeCost("5")


The way you did it is called a 'function expression' which lets you declare a function as part of an expression - It is often used when you want to pass functions around as arguments.
You will often see them used like

http.get("SomeUrl",function() {
//do some stuff on success
},function(){
//do some stuff on failior
}
);

Notice those functions don't have names that is because function expressions can be anonymous!

One thing to watch out for with function expressions is where you position them.


doStuff();
var doStuff = function(){};


That code is invalid because doStuff() doesn't exist at the point it is called.


doStuff();
function doStuff(){
}

Is valid because 'function declarations' are 'hoisted ' to the top by javascript whilst function expressions are not.
(edited 7 years ago)
Original post by INTit
Incase you wanted to know even more exciting stuff about JS functions - Javascript has a few different ways of declaring a function.
function orangeCost(arg){
}

orangeCost(5);
Is the most common way. That is called a function declaration. You've declared a function named orangeCost and are calling it passing '5' as the parameter.
var orangeCost = function(cost) {
console.log(cost * 5);
};
orangeCost("5":wink:
The way you did it is called a 'function expression' which lets you declare a function as part of an expression - It is often used when you want to pass functions around as arguments.
You will often see code like
http.get("SomeUrl",function(){
//do some stuff on success
},function(){
//do some stuff on failior
};
Notice those functions don't have names that is because function expressions can be anonymous!

One thing to watch out for with function expressions is where you position them.
doStuff();
var doStuff = function(){};
That code is invalid because doStuff() doesnt exist at the point it is called.
doStuff();
function doStuff(){
}
Is valid because 'function' declarations are 'hoisted ' to the top by javascript whilst function expressions are not.


This stuff isn't original to JS btw. C++ (and pretty much any other modern language) can have anonymous functions and function variables but without the god awful JS syntax. JS is only good for one thing and that's for using jQuery on HTML, apart from that, I ****ing hate it.
(edited 7 years ago)

Quick Reply

Latest

Trending

Trending