d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Any1 Wanna Help Me At Programming Beginner Level?
Add Reply New Topic New Poll
Member
Posts: 39,853
Joined: Apr 9 2009
Gold: 4,560.09
Jan 29 2014 11:54pm
The mutator for
speedInKperHour will check its parameter and use the parameter only if it is not a
negative number.


The mutator for driverStanding takes no parameters. It checks the
speed – if speed is 60 or less, standing will be “no ticket”, if speed is 80 or less,
standing will be “small ticket” and if speed is more than 80, the standing will be “big
ticket”


I'm not sure what Mutator Method to use for these two.

I'll give 50 FG to whoever help me with this.


This post was edited by MelsonX on Jan 30 2014 12:04am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jan 30 2014 12:23am
1 - You need to post all your relevant code that you have so far

2 - Don't pm people, just post it here

This post was edited by Eep on Jan 30 2014 12:25am
Member
Posts: 39,853
Joined: Apr 9 2009
Gold: 4,560.09
Jan 30 2014 12:37am
http://pastebin.com/YrKyLHRH
Heres the pastebin

or just down =)

/**
* Write a description of class Driver here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Driver
{
// instance variables
private String name;
private String driverLicense;
private int speedInKperHour;
private String driverStanding;

/**
* Default constructor
*/
public Driver()
{
name = "";
driverLicense = "";
speedInKperHour = 0;
driverStanding = "";
}
/**
* Second Constructor
*/
public Driver(String nName, String nDriverLicense, int nSpeedInKperHour)
{
name = nName;
driverLicense = nDriverLicense;
speedInKperHour = nSpeedInKperHour;
driverStanding = "";
}
/**
* Accessor for name
*/
public String name()
{
return name;
}
/**
* Avcessor for driverLicense
*/
public String driverLicense()
{
return driverLicense;
}
/**
* Accessor for speedInKperHour
*/
public int speedInKperHour()
{
return speedInKperHour;
}
/**
* Accessor for driverStanding
*/
public String driverStanding()
{
return driverStanding;
}
/**
* Mutator for name
*/
public void setName(String newName)
{
name = newName;
}
/**
* Mutator for driverLicense;
*/
public void setDriverLicense(String newDriverLicense)
{
driverLicense = newDriverLicense;
}

/**
* Display Method Driver details
*/
public void driverDetails()
{
System.out.println("Driver name: John James");
System.out.println("Driver License:" + 12456);
System.out.println("Speed:" + 70 + "K/H");
System.out.println("Driver Standing: small ticket");
}

}

This post was edited by MelsonX on Jan 30 2014 12:38am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jan 30 2014 01:24am
in the future, use code tags w/ proper indenting ;o
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Jan 30 2014 08:28am
Code
public String driverStanding( ) {
if ( speedInKperHour <= 60 ) {
return "no ticket";
}
if ( speedInKperHour <= 80 ) {
return "small ticket";
}
// Will return this by default if speed is over 80
return "big ticket";
}
Member
Posts: 22,502
Joined: Aug 5 2011
Gold: 0.00
Jan 31 2014 05:16pm
Didn't specify which language to use..
Code
#include <iostream>
#include <Windows.h>

void driver(int x);

int main(){
int speed;
std::cout << "Speed KPH: "; std::cin >> speed;
driver(speed);
Sleep(5000);
}

void driver(int x){
if (x <= 60)std::cout << "No Ticket\n";
else if (x <= 80)std::cout << "Small Ticket\n";
else std::cout << "Big Ticket\n";
}
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 31 2014 05:46pm
To actually answer your question:

You will commonly see the words "mutator" and "accessor" tossed around when referring to methods. Typical naming conventions for these contain the prefixes "set" for the mutator and "get" for the accessor.

Quote (MelsonX @ Jan 30 2014 12:54am)
The mutator for
speedInKperHour will check its parameter and use the parameter only if it is not a
negative number.



So, let's say you have some class Speedometer which has a private field speedInKperHour

Code
public class Speedometer {
private int speedInKperHour;

public Speedometer(){
}

}


You will presumably want to mutate(set) or access(get) the current speed. But you don't want to expose all of your fields to the user, so we make it private and expose only the methods

Code
public class Speedometer {
private int speedInKperHour;

public Speedometer(){
}

//accessor method
public int getSpeedInKperHour() {
return speedInKperHour;
}

//mutator method
public void setSpeedInKperHour(int currentSpeed) {
//set the current speed if currentSpeed is not negative
}
}


Above contains a stub for the mutator. Given the problem specs, what logic would you need to implement so that the mutator functions as needed?


Go Back To Programming & Development Topic List
Add Reply New Topic New Poll