Our system detected that your browser is blocking advertisements on our site. Please help support FoxesTalk by disabling any kind of ad blocker while browsing this site. Thank you.
Jump to content
General Smuts

Anyone any good at programming java?

Recommended Posts

Posted

package Coursework;

import stuff.from.other.people;

/**

* A simple model to complete homework.

*

*/

public class Cheating extends Plagerism

{

private static final int PRICE_OF_WORK = 50;

public Cheating()

{

super();

if(completed()) {

nextT = pub.nextPint(TOTAL_DRINKABLE);

}

}

:cool:

Quick questions only :thumbup:

Posted
Quick questions only :thumbup:

Im having a nightmare turning these 2 private mutators into a public mutator that incorporates both called setTime. It needs to have the same parameters which is where im having a problem. I can set parameters for hour but not for hour and minutes.

private String setHour (int hr)

{

if (hr >23)

{

return "0" + hr;

}

else

{

return "" + hr;

}

}

private String setMinute (int min)

{

if (min >59)

{

return "0" + min;

}

else

{

return "" + min;

}

}

Im sure its all very basic and im missing something obvious but ive been staring at it for a hour with no luck.

Any help would be awesome cheers.

Posted

private String setHour (int hr)

{

if (hr >9)

{

return "0" + hr;

}

else

{

return "" + hr;

}

}

private String setMinute (int min)

{

if (min >9)

{

return "0" + min;

}

else

{

return "" + min;

}

}

but now you need to filter out unacceptable inputs...

Posted

what do your functions even do?

If i supply 300 to setMintue it will return 0300, how is that helpful? Do you not need to typecast the return string too? Have you written these functions or been given them? Can you just post the question/brief?

Posted
public String setTime( int hr, int min ) {

return setHour( hr) + setMin( min );

}

am I missing something?

Yup - the lab calls for a certain order of steps to be followed and for certain things to be shown...the use of the modulo operator is encouraged and the ClockDisplay object is created from the two NumberDisplay objects.

I'll see if I can find a model solution to it LFF...but I don't think I have one so don't hold your breath.

I do have a whole host of other material that might be handy to you so fire me your email and I'll bung it up online for you to download and peruse :cool:

Posted
what do your functions even do?

If i supply 300 to setMintue it will return 0300, how is that helpful? Do you not need to typecast the return string too? Have you written these functions or been given them? Can you just post the question/brief?

1.4 Write a private mutator setHour which sets the value of the hour field from a suitable parameter. If the value of the parameter is within an appropriate range (between 0 - 23), the mutator should set the value of the hour field to the parameter value. If the parameter value is outside the appropriate range, the mutator sets the field value to 0.

1.5 Write a private mutator setMinute whose behaviour is similar to setHour but which uses the range appropriate for minutes (between 0 - 59). It sets the minutes field either to the value of the parameter if it is in range, or to 0 if it is outside the range.

1.6 Write a public mutator setTime, which sets the time from suitable formal parameters. The values of the parameters should be in the appropriate range : 0-59 for the minutes and 0-23 for the hour. If the value of a parameter is not in the required range, the value of the corresponding field should be set to 0 by the method. To avoid duplicating code, you should consider using appropriate local method calls.

These are the 3 questions im working from atm. The first 2 are the code i wrote above. 1.6 is the one im struggling with.

Posted
OK, like I say posting the question would be more helpful as I'm not sure whhat LFF is trying to do!

Question 1.4/5/6 above.

Its the parameters that im struggling with. That and me being an utter novice with Java.

Posted
These are the 3 questions im working from atm. The first 2 are the code i wrote above. 1.6 is the one im struggling with.

To be honest, the code is not the problem.

Think about how you would construct this, assembling the time using pieces of paper. The logical approach with which you assemble it all together is the same logical format that the code will be assembled - and the code you are currently using is incredibly basic but all the brackets and if-else statements make it look more confusing than it really is.

Seriously, draft out or flow map your ideas first and the coding will be much quicker.

OK, like I say posting the question would be more helpful as I'm not sure whhat LFF is trying to do!

I am - BlueJ labclass coursework Wk3 :D

Posted

1.4 Write a private mutator setHour which sets the value of the hour field from a suitable parameter. If the value of the parameter is within an appropriate range (between 0 - 23), the mutator should set the value of the hour field to the parameter value. If the parameter value is outside the appropriate range, the mutator sets the field value to 0.

private String setHour (int hr) {

this.hour = ( hr >= 0 && hr <= 23 ) ? hr : 0;

}

1.5 Write a private mutator setMinute whose behaviour is similar to setHour but which uses the range appropriate for minutes (between 0 - 59). It sets the minutes field either to the value of the parameter if it is in range, or to 0 if it is outside the range.

private String setMin (int min) {

this.min = ( min >= 0 && min <= 59 ) ? hr : 0;

}

1.6 Write a public mutator setTime, which sets the time from suitable formal parameters. The values of the parameters should be in the appropriate range : 0-59 for the minutes and 0-23 for the hour. If the value of a parameter is not in the required range, the value of the corresponding field should be set to 0 by the method. To avoid duplicating code, you should consider using appropriate local method calls.

public String setTime( int hr, int min ) {

setHour( hr );

setMin( min );

}

sorry if the "this." bit isn't right, I'm a javascript/actionscript etc. person myself so can't remember if this is how you set class variables in java (long time since I did any java!) but the rest will work fine and fits what the question is asking.

Posted
1.4 Write a private mutator setHour which sets the value of the hour field from a suitable parameter. If the value of the parameter is within an appropriate range (between 0 - 23), the mutator should set the value of the hour field to the parameter value. If the parameter value is outside the appropriate range, the mutator sets the field value to 0.

private String setHour (int hr) {

this.hour = ( hr >= 0 && hr <= 23 ) ? hr : 0;

}

1.5 Write a private mutator setMinute whose behaviour is similar to setHour but which uses the range appropriate for minutes (between 0 - 59). It sets the minutes field either to the value of the parameter if it is in range, or to 0 if it is outside the range.

private String setMin (int min) {

this.min = ( min >= 0 && min <= 59 ) ? hr : 0;

}

1.6 Write a public mutator setTime, which sets the time from suitable formal parameters. The values of the parameters should be in the appropriate range : 0-59 for the minutes and 0-23 for the hour. If the value of a parameter is not in the required range, the value of the corresponding field should be set to 0 by the method. To avoid duplicating code, you should consider using appropriate local method calls.

public String setTime( int hr, int min ) {

setHour( hr );

setMin( min );

}

sorry if the "this." bit isn't right, I'm a javascript/actionscript etc. person myself so can't remember if this is how you set class variables in java (long time since I did any java!) but the rest will work fine and fits what the question is asking.

If this works ill love you forever.

Posted

Oh sorry dude, change the "String" to "Void" for all of them as they don't return anything, they just set the values;

Like I say check the this.hr etc. too I'm not sure it will set the values right, I'm very rusty at java

Posted
Oh sorry dude, change the "String" to "Void" for all of them as they don't return anything, they just set the values;

Like I say check the this.hr etc. too I'm not sure it will set the values right, I'm very rusty at java

I've never been taught to use this. so i left it off of both and i now have no syntax errors.

I love this forum!

Thanks both.

Now onto 1.7.

Posted

ah right sod it then! you need this. if it's in a class I think like

class setTime {

int hr, min;

private void setHour (int hr) {

this.hour = ( hr >= 0 && hr <= 23 ) ? hr : 0;

}

private void setMin (int min) {

this.min = ( min >= 0 && min <= 59 ) ? hr : 0;

}

public void setTime( int hr, int min ) {

setHour( hr );

setMin( min );

}

}

who knows the mind of java, not the best language to be honest, probably as complex as C++ in some ways and way less powerful

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...