So in java I had the following implementation:
public abstract class PFigure implements Comparable
and the java implementation for my inheritance and polymorphism:
public class PFigureList
{
private final int MAX_VEHICLES = 9;
private PFigure list[] = new PFigure[MAX_VEHICLES];
private int count = 0;
/**
Adds a PFigure to the list if the list is not full and increments the count.
#param myFig The "vehicle" PFigure that is going to be added to the list
*/
public void add(PFigure myFig)
{
if(count <= 9)
list[count++] = myFig;
}
/**
For every figure in the list, it calls their hide() function, their
polymorphic move() function, and then their draw() function to show where
they are now.
*/
public void move()
{
for(int i = 1; i < count; i++)
{
list[i].hide();
list[i].move();
list[i].draw();
}
}
Now what I want to accomplish is this except very similar to it in C++. Here is my code:
void VBotList::Add(VBot * add)
{
vbot[count++] = add;
}
void VBotList::Move()
{
/*for(int i = 0; i < list.GetCount(); i++)
list.GetValue(i)->Move();*/
for(int i = 0; i < count; i++)
vbot[i]->Move();
}
class VBotList
{
private:
int count;
VBot * vbot[50];
public:
VBotList() : count(0){ }
void Add(VBot * vbot);
void Move();
void Show();
};
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
static VBotList * list = new VBotList();
And my attempt to implement it:
private: System::Void speedTimer_Tick(System::Object^ sender, System::EventArgs^ e) {
list->Move();
Invalidate();
speedTimer->Interval = speedTrackBar->Value;
}
private: System::Void vbotAddButton_Click(System::Object^ sender, System::EventArgs^ e) {
if(comboBox1->SelectedIndex == 1)
{
VBot * x = new BillyBot(System::Convert::ToInt32(textBox1->Text), System::Convert::ToInt32(textBox2->Text), panel1);
list->Add(x);
}
}
private: System::Void panel1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
list->Show();
}
My goal here was to take a VBot object which is stored in a VBotList, which was like my PFigureList of PFigure objects in java. I have no clue why, but I cannot get it to actually display my object on the panel. I had to make the initialization of the VBotList static in order for it to not give me error messages. Am I missing something completely obvious or am I just doing something incorrect about displaying the object? Any hints, advice, or a slap on the hand about my code would be great.
Show() Basically just displays the image. I'll post the code if needed.
You work in C++/CLI, so you're class must be managed too, example :
ref class Bot
{
public:
Bot()
{
}
};
ref class VBotList
{
private:
int m_count;
array<Bot ^> ^vbot;
public:
VBotList() : m_count(0), vbot(gcnew array<Bot ^>(50))
{
}
void Add(Bot ^newBot)
{
vbot[m_count++] = newBot;
}
int getCount()
{
return m_count;
}
};
And next :
private:
VBotList ^list;
public:
Form1(void)
{
InitializeComponent();
list = gcnew VBotList();
label1->Text = System::Convert::ToString(list->getCount());
}
// ...
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Bot ^bot = gcnew Bot();
list->Add(bot);
label1->Text = System::Convert::ToString(list->getCount());
}
^ declare a handle to a managed pointer.
Related
I am a Java beginner. My problem is, for example:
Fire: DMG +1
Stone: DEF +1
By combining to:
Fire-stone: fire + stone, and inherit both their properties (DMG 1, DEF 1).
Flame: fire + fire, and inherit 2 fire properties (DMG +2).
I've played around with classes and interfaces but doesn't seem to work. It seems to me that Java doesn't support multiple inheritance but multiple interfaces. I wonder how I could code each class/interface for this to work?
public static int DMG, DEF = 0;
public static String DESC = "";
interface fire {
DMG = 1; }
interface stone {
DEF = 1; }
class firestone implements fire, stone {
DESC = "Born in fire and stone";
//DMG and DEF calculations
}
You better use composition, here is what you could do:
public class Main {
public static void main(String[] args) {
Tool firestone = new Tool(new Fire(), new Stone());
}
private static interface Component {
int getDmg();
int getDef();
}
private static class Fire implements Component {
#Override
public int getDmg() {
return 1;
}
#Override
public int getDef() {
return 0;
}
}
private static class Stone implements Component {
#Override
public int getDmg() {
return 0;
}
#Override
public int getDef() {
return 1;
}
}
private static class Tool implements Component {
List<Component> components;
int dmg;
int def;
public Tool(Component... component) {
components = new ArrayList<>();
components.addAll(Arrays.asList(component));
dmg = 0;
def = 0;
for (Component c : components) {
dmg += c.getDmg();
def += c.getDef();
}
}
#Override
public int getDmg() {
return dmg;
}
#Override
public int getDef() {
return def;
}
}}
My implementation may be an overkill :P but it is extendable and you add more components that are more and more complicated.
this is the qa:
Define a class called MoreSpeed which extends the following class, and which provides a new method called incSpeed() which adds 1 to the inherited variable length.
this is my answer:
public class Speed {
private int length = 0;
public int getSpeed () { return length; }
public void setSpeed (int i) {
if (i > 0) {
length = i;
}
}
}
public class MoreSpeed extends Speed {
private int length;
public int incSpeed() {
return length+1;
}}
its says that the syntax is good but the class operation is wrong.
please help me,thanks.
No. You are shadowing the length from Speed. Instead, implement incSpeed with getSpeed() like
public int incSpeed() {
return getSpeed() + 1;
}
If you are supposed to modify it as well then use setSpeed(int) to do so
public int incSpeed() {
int s = getSpeed() + 1;
setSpeed(s);
return s;
}
I am trying to fill UCFCourse courseOne in my constructor with a courses[] object in fillWithCourses().UCFCourse courseOne does populate outside of the constructor but will not go into it.
public class UCFSemester<courses> {
private static UCFCourse courseOne;
private static double totalSemesters;
private static double completionTime;
static boolean fillSemester = true;
public UCFSemester(UCFCourse courseOne, UCFCourse[] coursetwo) {
this.courseOne = courseOne;
}
public static UCFCourse getcourseOne() {
return courseOne;
}
public static void setCoursesone(UCFCourse courses) {
courseOne = courses;
}
public static void fillWithCourses(UCFCourse courses[], int l) {
int x = 0;
while (fillSemester) {
for (int n = 0; n < 5; n++) {
if (x != n && courses[x].getCourseLevel() < courses[n].getCourseLevel()) {
setCoursesone(courses[x]);
}
}
fillSemester = false;
}
}
}
Side question.How can I access this all in a non-static way?I need the entire thing to be non-static but no matter what I do I can't get it.Thanks!
You can simply do it by creating a List like this:
public class UCFSemester {
private List<UCFCourse> courseList = new ArrayList<>();
public UCFCourse getCourse(int index) {
return courseList.get(index);
}
public void addCourses(UCFCourse[] courses) {
for(int x = 0; x < courses.length; x++) {
courseList.add(courses[x]);
}
}
}
Here, I'm assuming that you are passing the UCFCourse[] array with all the course details that are there in that particular semester.
addCourses() function will take this array and then add all the corresponding courses to the List.
getCourse() function will return you any particular course from the List (Using Index). You can also modify the search in any way you want.
How do you mirror a function on x-Axis in Apache commons math, respectively set f() = -f()?
I found out so far, that you can add functions with FunctionUtils class and i guess i could
do a workaround by taking some points, set y-values negativ and interpolationg new Function,
but that seems a little cumbersome to me. Is there a simpler way?
As all functions are interfaces in org.apache.commons.math3.analysis you can wrap every function you want to invert into an anonymous object implementing that interface.
Here are three examples which should get you started:
/**
* Created for http://stackoverflow.com/q/22929746/1266906
*/
public class MinusFunction {
public static BivariateFunction invert(final BivariateFunction function) {
return new BivariateFunction() {
#Override
public double value(double x, double y) {
return - function.value(x,y);
}
};
}
public static MultivariateFunction invert(final MultivariateFunction function) {
return new MultivariateFunction() {
#Override
public double value(double[] point) {
return -function.value(point);
}
};
}
public static MultivariateMatrixFunction invert(final MultivariateMatrixFunction function) {
return new MultivariateMatrixFunction() {
#Override
public double[][] value(double[] point) {
final double[][] value = function.value(point);
for (int i = 0; i < value.length; i++) {
for (int j = 0; j < value[i].length; j++) {
value[i][j] = -value[i][j];
}
}
return value;
}
};
}
}
I have a public integer variable (MainReg) in my Counter Class. I want to get value of this variable and set it in my JComponent class. Here is piece of my JComponent class:
public class Komponent2 extends JComponent implements ActionListener
{
Counter counter3;
.
.
.
int a = counter3.valueOf(MainReg);
But it doesn't work. I tried also:
int a = valueOf(counter3.MainReg);
int a = counter3.valueOf(counter3.MainReg);
int a = counter3.MainReg;
But it still doesn't work. How can I get this variable? Thanks for helping me.
EDIT
Here is my Counter class:
import java.util.Observable ;
public class Counter extends Observable
{
public int MainReg;
public int CompareReg;
public Mode countMode;
public boolean OVF;
private int a=0;
public Counter()
{
OVF=false;
}
public void setCompareReg(int dana)
{
CompareReg=dana;
}
public void setMainReg(int dana2)
{
MainReg=dana2;
}
public void setMode(Mode countMode)
{
this.countMode=countMode;
}
public void Count()
{
if (countMode==Mode.UP)
{
MainReg++;
OVF=false;
if (CompareReg < MainReg)
{
OVF=true;
MainReg=0;
setChanged();
notifyObservers();
}
}
else if (countMode==Mode.UPDOWN)
{
if(MainReg >= CompareReg)
{
a=MainReg;
MainReg--;
OVF=true;
}
else
{
if(MainReg >= a)
{
MainReg++;
OVF=false;
}
else
{
MainReg--;
if(MainReg==0)
{
a=0;
}
OVF=false;
}
}
}
else if (countMode==Mode.CONTINOUS)
{
MainReg++;
OVF=false;
if (65536 < MainReg)
{
MainReg=0;
OVF=true;
}
}
}
}
Well I see two ways you can do this.
Your MainReg integer is public, you could simply use int i = counter3.MainReg;
Or you could create a getMainReg() method in your Counter class. Then call it from whatever class.
EX:
public int getMainReg() {
return this.MainReg;
}
Give your Counter class getter methods, and then call them when you need to access their values. i.e.,
public int getMainReg() {
return mainReg;
}
public int getCompareReg(){
return compareReg;
}
public Mode getCountMode() {
return countMode;
}
And make your fields all private. Also your code should obey Java naming rules: variable names should begin with lower-case letters.
Also be sure that you've initialized your counter variable in the class that uses it, either by creating a new instance, or if appropriate, passing in a valid instance in a constructor or method parameter.