Declare a constant class field - string

I'm supposed to declare a class field for a federal tax rate, which is a constant, with a value .07. How can I achieve this?

Since you didn't specify a language, I'll throw in a Java solution
public class YourClass {
public static final double TAX_RATE = 0.07;
}
In Java naming convention, you name your constants with all upper casing and use _ to separate words instead of the normal camel casing.
the final keyword, is to make it, well, final - meaning unchangeable
Anything not withing a block like a constructor or a method is considered a field.

Related

Is it possible to change keyword for cross referencing between grammar rules/objects in Xtext?

When I want to make cross referencing between grammar rules in Xtext work, I need to use keyword name for that. E.g.:
Constant:
name=NAME_TERMINAL "=" number=Number;
ConstUsage:
constant=[Constant | NAME_TERMINAL];
Is it possible to change this word to another one (e.g. id) ? I need it e.g. in case when I have rule, which uses parameter name for something else.
you can use a custom implementation of IQualifiedNameProvider e.g. by subclassing DefaultDeclarativeQualifiedNameProvider.
public class MyDslQNP extends DefaultDeclarativeQualifiedNameProvider{
QualifiedName qualifiedName(Element e) {
Package p = (Package) e.eContainer();
return QualifiedName.create(p.getName(), e.getId());
}
}
see https://dietrich-it.de/xtext/2011/07/16/iqualifiednameproviders-in-xtext-2-0.html for the complete example

UML class diagram dependency or association

I'm not really sure about how to distinguish whether I should define a relationship as dependency or association for certain cases.
For example,
class AttendanceSheet {
Map<String> students;
boolean[] attend;
public void addStudent(Student s)
{
students.add(s.getName(),s.getStudentNumber());
}
public void checkAttendance(String name) { //... }
}
class Student {
private String name;
private int staffNumber;
//more information such as address, age, etc..
Student(String n, int sn)
{
name = n;
studentNumber = sn;
}
public String getName()
{
return name.clone();
}
public String getStudentNumber()
{
return studentNumber;
}
}
For this case, would Student and Association have association or dependency?
This is because I'm not sure whether the association must have the actual reference of the object or it suffice to just have certain information that can reach the object (since student id and number is far more enough to know find out which student object it is directing to).
In your case the <<uses>> is sufficient, because you don't have actual properties of type Student in AttendanceSheet.
As a side note: not using object references and instead just having the studentNumber is - to say the least - an odd design. But I don't know the context.
On the business level those objects are related, but there is no single preferred method of diagramming this relationship.
Please see Section 9.5.4 of UML specification for more details on the topic, especially Figure 9.12
To be specific those two notations are semantically equivalent (I'm ignoring irrelevant details):
In the first one to keep a traceability you can use an explicit Dependency pretty much the way you did.
One can also consider students as a Shared Aggregation, however it might be also considered an overkill. Not necessary, just showing a possibility for an answer completeness.
You may also consider Qulified associations to indicate a reference to the Student is based on their specific properties. This is pretty much closest to your need. Sorry, I don't know how to achieve such notation in my tool, but you can find more details in Figure 11.37 in Section 11.5 of the aforementioned specification.

Using class constants for [Range] attribute

For a unit test I want to use the Range attribute from NUnit to test inputs to a function in a range. The lower and upper limits of this range are coded into constant properties of a (Singleton pattern) class. I would like to specify the starting point and end point of the Range attribute with the class properties, something like this:
[Test]
public void sometest([Range(MyClass.LOWER_LIMIT,MyClass.UPPER_LIMIT)] int var)
{
//Do something and assertive with the nice variable
}
However, this approach does not work. Although it is not clear from the documentation itself, it seems that the Range attribute must be provided constant variables. While my class constants are static properties with only get defined, this does capture a constant variable.
I posted and answer to this question, but is this really the way to set the range parameters based on class constant in NUnit? Or is there a more elegant solution?
The following example demonstrates how one can use (constant)properties from a class as values used with the Range attribute from NUnit.
const int LO_LIM = 1;
const int HI_LIM = 10;
[Test]
public void assertConstantsCorrect()
{
//Will fail if constants change during development!
Assert.AreEqual(MyClass.LOWER_LIMIT,LO_LIM);
Assert.AreEqual(MyClass.UPPER_LIMIT,HI_LIM);
}
[Test]
public void sometest([Range(LO_LIM,HI_LIM)] int var)
{
//Do test
}
The first step is to define constants in your test class, as the Range attribute only works with constants. These constants take the same values as the constants defined in the properties of your class.
Second is a Test created to verify that they correspond. If at a later date and time the constants in MyClass change, the failure in this test will notify you of this change. Take note that if this test does not pass, any other test using those constants can be regarded as invalid as they rely on false asumptions!
Lasty are your actual tests that use those values in the [Range( start, end)] clause.
Alternatively, you can also make use the [TestFixtureSetUp] attribute instead of the [Test] attribute for the assertConstantsCorrect() method to make all tests in the fixture fail in case the assertConstantsCorrect() fails.
Yet another alternative is to make a custom attribute to work for specific methods you as programmer annotate and make those methods fail when assertConstantsCorrect() fails.

What is the most efficient way of setting variables in another class using C#

I am polling the event queue and setting variables in another class depending on the event result.
while(!fin){
pollEvents();
}
And in the event handler:
case Sdl.SDL_MOUSEMOTION:
game.setdx(e.motion.y);
game.setdy(e.motion.x);
break;
I am new to C# from Java. My understanding is that these values will be passed by value rather than reference and am wondering if it would be better to:
make dx and dy in the other class public and simply set the values
use pointers to dx and dy and set the values using them
Figured it out so thought i'd share. Properties are applicable to both classes and structures... I was under the impression they were solely for structures. Anyway, from what I understand, properties are like direct assignments. Syntax:
class ThatWillStoreTheVal{
private bool ford=false;
//seter/getter method called a property
public bool fordP{
set{ford=value;}
get{return ford;}
}
}
class ThatSetsTheProperty{
private ThatWillStoreTheVal ob;
//the assignment. Note that the method like name is called not the variable name
...
if(e.key.keysym.sym==Sdl.SDLK_e) ob.fordP=true;
...

Why can't I use DateTime.MinValue and int.MinValue as optional values in C# 4.0?

Can someone explain what's wrong with the following method signature written using C# 4.0?
public void Test(string arg1 = string.Empty, DateTime arg2 = DateTime.MinValue){}
I understand the difference between "" and string.Empty in terms of compile time checking but surely the way that optional parameters have been implemented in C# 4.0 is pretty inadequate if you can't declare a reasonable value type null style comparisson?
Because DateTime.MinValue and DateTime.MaxValue aren't compile time-constants -- they're readonly fields that are initialized at run time by DateTime's static constructor.
See the difference between const fields (which are compile-time constants) and readonly fields (which aren't): What is the difference between const and readonly?
You can use as parameter values only literal values, constant values and new object instances.

Resources