Jump to content

TheHoijf

Member
  • Posts

    32
  • Joined

  • Last visited

Awards

This user doesn't have any awards

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

TheHoijf's Achievements

  1. Nuluvius, you aren't being helpful, only pointing out what you consider mistakes although I would consider this more in the realm of technicalities. Why so aggressive anyway? My information has at no time been incorrect and you seem to be picking it apart for the sake of it. Why don't you suggest good starting points? Make a comprehensive summary like I did and enlighten us all? If I am so deeply mistaken I would definitely appreciate the clarification. So far, you have failed to teach me anything. The patterns I listed are used regularly and I was taught them at university so I thought I would link them. The professors in my university consider them good enough to center a module around them, why don't you? They may have their flaws but it is good to know about them because they have a tendency to open one's eyes to new practices, show possibilities. Allow me to clarify. I'd like to start off by saying, books have been written about this. Many BIG books. I am trying to be concise, please use your common sense when reading this. If concise just won't cut it and you need multiple pages of text then please, go get a book (I am talking to you Nuvilius). Overloading Definition from BeginnersBook You can do whatever you want in each method, for all intents and purposes they are completely separate methods within the class, just like any other randomly chosen methods in the class. The similarity between them (the name) is only for the reader. The point of using overloading is to associate methods which share some similarities. You don't need to use it this way, but it is intended to be used this way. Example: private int add(int[] a) { int b = 0; for(int i = 0; i < a.length; i++) {b += a[i];} return b; } private int add(int a, int b, int c) { return a+b+c; } You can also have one draw a cow on the screen and one calculate prime numbers, if you really want to. In this sense, I would say Nuvilius has a poor grasp of the practice. While the possibility is there, that is not its use case and shouldn't be used that way. Why do I rarely use it? Because, I prefer to edit the name. For example, here I would have add_A and add_B. Why? Well I find it easier to find my way through code this way. The eclipse auto highlight makes it easy to find what I am looking for if I differentiate things when possible. I could use Javadoc, which I do, but that is different. Getters and Setters Everything I said in my first post about them remains correct. They should always be used on serious work. However if you are testing something, I personally don't think its worth the time. They only ever become useful once the calls to the variable are so numerous that you would actually save time by using getters and setters. The more you access a given variable the more necessary it is to have getters and setters. What is the fundamental reason for using them though? Well, if you have a variable in class A which you want to access from classes B, C, D. You never know what the future holds for our variable and any change to it would mean you'd also need to change classes B, C and D. To avoid this, you can implement a getter and instead of changing B, C and D you can just edit the getter. In the long run it will save a huge amount of time. On tiny projects however it will use more time than it takes. In my experience this has been the case, maybe I just plan things out better than most. Sometimes people also use them as a point for validation or other tasks, I however prefer to do this on the other end. When resources are limited (weak computers) you want to minize processing. If classes B and C need validation on the variable but not D then I would create a validation class which would be called by B and C, in this way, D wouldn't waste time validating the variable since he doesn't need to.
  2. That's what I meant. Sorry for the confusion. Definitely better said than me ^^. Getters and setters are a must. Even when you don't plan on making changes to something or ever coming back to it, you should use them just in case. Admittedly, I have ignored them at times without any loss of functionality or time but only in sub thousand line programs which were made for use by me and solely by me (even here it is silly but whatever, it worked out). I would never skip on it when working on professional or academic work and would advise others to do the same. They have become a standard for a reason!! As for the " I sometimes see 2 methods inside a class using the same name; how does that work? " section; it can mean multiple methods which do the same thing but require different variables could be made to share a method name thus avoiding confusion from the programmer's end. In that sense it is sometimes beneficial. I rarely if ever use it but the practice does take place so I thought I'd mention it. I think for a first year exam, knowing the little block I wrote + any corrections made would be a good start!! Keep in mind, Cookiecrumbles222 is only looking for some relatively nooby information!! He will have his whole career to delve into intricacies. For the time being, surface knowledge is certainly adequate. I was only taught about public / private and void / returns in second year!! I came back to mention something else though. I am unsure whether this is in the scope of OO but it won't hurt to know about it. Singleton Pattern Factory Pattern Observer Pattern Command Pattern These are programming patterns which you often encounter in OO. They are very much worth looking into because not only will you learn about the patterns but other underlying aspects of OO. Go to Stack Overflow and check out some threads there concerning OO. The forum is catered to users with at least medium level experience but I sure found it helpful when I had just started.
  3. This is a java program which allows the user to set a shutdown timer. 150 lines. The shutdown timer can be canceled restarted and has a progress bar so you can see how much time is left till shutdown. Made this a while ago but thought I would share it. If you like it I have a bunch of other stuff I can share. I apologize for any rude comments or variable names. I don't always keep things polite in my code : / **I quickly reread the code and it could be reduced quite a bit. A few lines by replacing the mouse listener with a mouse adapter. Some black lines. Could fit under 100.** public class MainWindow { private JFrame frame; private boolean on = false; private JProgressBar progressBar; private Color NeonGreen = new Color(0,255,128); private Color NeonBlue = new Color(0,200,255); private Color NeonRed = new Color(255,102,102); private Color Fade = new Color(45,45,45); private long start; private long end; private long cur; private long estimate; private float calc; private Timer timer = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cur = System.currentTimeMillis() - start; calc = (float) cur / (float) estimate * 100; progressBar.setValue((int) calc); }}); public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() {try{MainWindow window = new MainWindow();window.frame.setVisible(true);} catch(Exception e){e.printStackTrace();}} }); } public MainWindow(){initialize();} private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 259, 121); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setTitle("hPower"); frame.setAlwaysOnTop(true); frame.getContentPane().setBackground(Color.black); frame.getContentPane().setLayout(null); JLabel lblCountdown = new JLabel("Countdown"); lblCountdown.setBounds(10, 11, 121, 14); lblCountdown.setForeground(NeonGreen); frame.getContentPane().add(lblCountdown); JComboBox comboBox = new JComboBox(); comboBox.setBounds(79, 8, 164, 20); comboBox.setForeground(NeonGreen); comboBox.setBackground(Fade); comboBox.setFocusable(false); frame.getContentPane().add(comboBox); JButton btnStartStop = new JButton("Start"); btnStartStop.setBounds(79, 58, 103, 23); btnStartStop.setBorder(null); btnStartStop.setFocusable(false); btnStartStop.setForeground(NeonGreen); btnStartStop.setBackground(Color.black); btnStartStop.setBorder(new LineBorder(NeonGreen)); frame.getContentPane().add(btnStartStop); btnStartStop.addMouseListener(new MouseListener() { @Override public void mouseExited(MouseEvent arg0) {Hover(arg0, arg0.getComponent().getForeground(), arg0.getComponent().getBackground());} @Override public void mouseEntered(MouseEvent arg0) {Hover(arg0, arg0.getComponent().getForeground(), arg0.getComponent().getBackground());} @Override public void mouseClicked(MouseEvent arg0){} @Override public void mousePressed(MouseEvent arg0){} @Override public void mouseReleased(MouseEvent arg0){} }); //Filling combo comboBox.addItem("5 minutes"); comboBox.addItem("15 minutes"); comboBox.addItem("30 minutes"); comboBox.addItem("45 minutes"); comboBox.addItem("1 hour"); comboBox.addItem("1 hour 30 minutes"); comboBox.addItem("2 hours"); progressBar = new JProgressBar(); progressBar.setBounds(10, 36, 233, 14); progressBar.setBackground(Fade); progressBar.setForeground(NeonGreen); progressBar.setFocusable(false); progressBar.setBorder(new LineBorder(NeonGreen)); frame.getContentPane().add(progressBar); btnStartStop.addActionListener(new ActionListener() { @SuppressWarnings("unused") @Override public void actionPerformed(ActionEvent arg0) { if(on) { try { Runtime runtime = Runtime.getRuntime(); Process proc = runtime.exec("shutdown -a"); btnStartStop.setBackground(NeonGreen); btnStartStop.setBorder(new LineBorder(NeonGreen)); btnStartStop.setText("Start"); on = false; timer.stop(); }catch(IOException e){} }else { try { Runtime runtime = Runtime.getRuntime(); Process proc = runtime.exec("shutdown -s -t "+timer(comboBox.getSelectedIndex())); btnStartStop.setText("Stop"); btnStartStop.setBackground(NeonRed); btnStartStop.setBorder(new LineBorder(NeonRed)); start = System.currentTimeMillis(); end = System.currentTimeMillis() + timer(comboBox.getSelectedIndex()) * 1000; estimate = end - start; on = true; timer.start(); }catch(IOException e){} } } }); } private int timer(int index) { String time; if(index == 0){time = "300";}else if(index == 1) {time = "900";}else if(index == 2){time = "1800";}else if(index == 3) {time = "2700";}else if(index == 4){time = "3600";}else if(index == 5) {time = "5400";}else{time = "7200";} return Integer.parseInt(time); } private void Hover(MouseEvent arg, Color color1, Color color2) {arg.getComponent().setBackground(color1); arg.getComponent().setForeground(color2);} }
  4. If you want a more in depth explanation about anything specific go ahead and ask.
  5. I may have made some mistakes, no one is perfect. So take it easy when correcting me. Abstraction What you are talking about is called abstraction. Wikipedia (Abstraction): When speaking of abstraction, the "higher" the abstraction layer is the farther away from the raw code you are. Generally speaking high levels of abstraction are a good thing especially when working in a team since it makes global understanding a given. Only the person who made a piece of code knows exactly how it works and others may not have time to read the hundreds of lines and make sense of them. To this end a method or class can be made with documentation and other programmers can interact with the software as they see fit. Classes and methods serve to make a program easier to understand and minimize duplicate code. They can be used for additional purposes but when you are getting started that's all you really need to worry about. A simple example is: I recently wrote a version control program and thought I'd add RGB functionality to it. Instead of going over every window and component in the program, I editted the class responsible for designing elements and made it call an RGB generator. The entire RGB logic is stored in its own class, it is built of a bunch of methods. The class can be taken and placed in other programs since it is self sufficient. public class HSLColorHandler { private float Q; private float curQ = 0; private Color newC() { float hslF[] = new float[3]; hslF[0] = (curQ/Q)*0.7f; hslF[1] = 1f; hslF[2] = 0.6f; curQ += 1; int l = makeRGB(hslF); return new Color(l); } private int makeRGB(float[] hsl) { float alpha = 1.0f; float h = hsl[0]; float s = hsl[1]; float l = hsl[2]; float q = 0; if (l < 0.5) q = l * (1 + s); else q = (l + s) - (s * l); float p = 2 * l - q; int r = (int)(255*Math.max(0, HueToRGB(p, q, h + (1.0f / 3.0f)))); int g = (int)(255*Math.max(0, HueToRGB(p, q, h))); int b = (int)(255*Math.max(0, HueToRGB(p, q, h - (1.0f / 3.0f)))); int alphaInt = (int)(255*alpha); return (alphaInt << 24) + ( r << 16 ) + ( g << 8 ) + (b); } private float HueToRGB(float p, float q, float h) { if (h < 0) h += 1; if (h > 1 ) h -= 1; if (6 * h < 1){return p+((q-p)*6*h);} if (2 * h < 1 ){return q;} if (3 * h < 2){return p+((q-p)*6*((2.0f/3.0f)-h));} return p; } public Color getColor() {return newC();} public void setCurQ(int curQ){this.curQ = curQ;} public void setQ(int Q){this.Q = Q;} } But then how do I access it and what exactly are the benefits? To access it you need to first create an instance of the class. You do this by writing "classTitle instanceName = new classTitle(parameters);" in this case it would be: HSLColorHandler hsl = new HSLColorHandler(); This is because there is no constructor. We will see what a constructor is in a minute. Once you have the instance (hsl) you can then access the class methods and variables in this manner: instanceName.methodName(parameters); in our case, if we want to get a new RGB color we would write: Color rgb = hsl.newC(); This brings up to returns. You notice I am saying the color rgb is equal to this method "newC()" which I am running. newC() is a method with a return. This means it will run and when it is done, will return a value. In this case it is the rgb color. You can do this with any variable EVEN with class instances. What is a constructor? In this class the constructor would have been: public HSLColorHandler(parameters){do stuff when an instance of this class is created.} The constructor runs immediately when the class is instantiated. It can request parameters like a method and run logic like a method. You can think of it as the "Main" method of the class. When should you use a constructor? Like many things in programming, its not necessarily about better or worse code. It can just be about how you like to do things and what you consider more efficient / practical / functional. Many programming techniques are more due to the programmer's style than to a tangible benefit at run-time. To answer the question, if you want something to happen when a class is first created then make a constructor for it and handle it all in there. Where do I put the constructor? It goes in the class, in this class the HSLColorHandler constructor would be inside HSLColorHandler. So whats the point of having methods? While classes serve as a program wide solution, methods are a more local layer of abstraction. They allow you to split a task within a class into sub tasks. They much like the class are reusable and are simply called this way: methodName(parameters); Methods can return variables as stated earlier. I am calling a method but it is not working!! Well you need to watch out for something. When accessing methods or variables from another class you need to make sure what you are accessing is public. There are other ways but they are considered hacky and are out of the scope of this brief overview. You see in the class I have attached, some methods are private, others are public. Same for variables. Anything private is only accessible from within the class itself. Public stuff can be reached from anywhere. This is why you should always use getters and setters which I'll go over in a second. Once more, the use of private public can come down to the programmer's preference. Especially at the start of their career however as you progress you will realise how useful it is to manipulate them correctly until it becomes a necessity. It is good practice to begin now. When should a method / variable be private? If you only plan on accessing it from within the class then there is no reason for it to be public!! It will make accessing the class from the exterior more complicated. Remember the goal is to keep as much of the stuff happening inside the class right where it is and not burden the programmer with its complexity. To this end minimizing interraction is a must. Look at the code I attached. I don't require a user to access each individual method, rather I handle it all within the class. The flow of data is newC() >> makeRGB(float[] hsl) >> HueToRGB(float p, float q, float h) >> newC(). So when should I make it public? To interact with the class you do need the means to do so. Make public what needs to be accessed from the exterior, try to keep the flow of data and logic of the system contained to the class. Variables should almost never be public!! Unless you are purposefully hacking something together which is not advised either. We all do it occasionally though. I sometimes see 2 methods inside a class using the same name; how does that work? I forget the terminology but it is essentially a run-time switch. As long as all the methods sharing a name have different parameters you are good to go. You will need to access the methods with care though since parameters input to you method call will select which method is called. This is a more efficient (in terms of resources) way of handling certain situations. What is the point of Getters and Setters? These are methods, inside a class and they replace direct access to a variable. Yes you could make the variable public but this is against the principle of abstraction. There are a bunch of reasons for using getters and setters, just google it. In the meantime trust me. Use them!!! Hope this helped, I am not sure how much you knew and needed to know. I have just explained what I have noticed people had trouble with. **I am also a student, in fourth year. I know I probably messed some stuff up so correct me please!** I also barely scraped the surface but it should be a good starting point. Further reading: Abstraction How to design OO architecture OO paradigm
  6. Sorry then. I don't have anything to run it on and fix it but you can just comment lines out and see if it runs. One line at a time and once it runs you've found your mistake. You can then trace it back and fix it. If you or someone haven't resolved the issue by the time I have VisualStudio I'll send you my solution. GL
  7. I also advise using VisualStudio. Its what I used to use and its amazing.
  8. I don't have anything to run the program from my end. Its probably got errors but I commented where I made changes so you can just check those parts. int main() { //Sieb des Erathostines int size = 200000; int array[size]; //Changed int i, j; int posNumber = 0, negNumber = 0, sum = 0; //Removed here for(i = 0; i < size; i++) //Changed { array[i] = -1; } for(i = 2; i < size; i++) // Changed { if(array[i - 2] == -1 || array[i - 2] != 0) { for(j = i; j < size; j = j + i) // << this is adding I, then I+I, then I+I+I and may be causing the issue? { if(j == i && j < size) // << changed this { array[j - 2] = 1; posNumber++; }else{ if(array[j - 2] == -1) { array[j - 2] = 0; negNumber++; } } } j = 0; } } printf("\n Anzahl der Primarray:%d\n Anzahl der NICHT-Primarray:%d\n Gesamtmenge der array %d gleich size: %d?", posNumber, negNumber + 1, posNumber + negNumber + 1, size); for( i = 0; i < size; i++) { if(array[i] == -1) { printf("\n Es wurden nicht alle array bearbeitet! array[%d]", i + 2); }else{ //if(array[i] == -1) sum = array[i] + sum; } } return(8); }
  9. Nope, I was using this cable to connect my desktop to the monitor a week ago. I can't even reach 1080p right now. 800p is maximum.
  10. That must be for DVI-D single link. I am talking about dual link. Thank you for reply tho
  11. Run it and send me the error message please. It'll give me some much needed insight.
  12. Yes its enough BUT If you have sensitive data then encrypt it and get hardcore security.
  13. Why not go on stackoverflow? You're probably having exactly that as a problem as well. Your array is only allowed to store so much data before capping out. Maybe?
  14. As long as you don't visit dodgy websites and I mean seriously obscure stuff; you should be fine. I don't even have the windows one on and haven't had a virus since I got a little to curious when I was 16.
×