• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need suggestion on designning a assignment  RSS feed

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on 1 of my assignment given by my professor. I need to design analog clock. As per earlier guideance i have break this problem in 3 parts 1) seconds 2) Minutes 3) Hour.

I am trying to implement one by one. I also earlier tried with page and pencil approach and once initial design complete i have change into the code. Seconds and Minute part is implemented and running sucessfully

Kindly look into my code and suggest the improvements and also whether i am following oop style or not.

Explanation:

1) I develop ClockProperties class which have general properties of the clock which will be shared by all other classes. This class having all get and set methods and 2-3 generic methods which will be used by other classes to move.

2)Second is Seconds class which is using method and paramertes from the clock properties class.



3rd is MyClock class which create simple GUI and call above classes. I also used inner class into this which having component method.


 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can anyone please suggest? I only need suggestion so that i can improve my code.. I already completed my assignemnt..
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's way too much code to look over there and your question is too vague.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I modified my post and remove my one generic class which have common properties and also some of the lines from the existing code i pasted.

I hope its length is reduced now very much and may be eaiser to review it or give any valuable comments for improvement so that i correct them in my next project.
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain why the Seconds class extends ClockProperties? In what way is a Seconds object a ClockProperties object? Wouldn't it be more reasonable for a Seconds object to contain a reference to a ClockProperties object?

(Since I don't see any information about ClockProperties, I'm in the dark about what it is.)
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
classProperties having information about properties like frame width , height , start position , second , minute , hour and common methods like to increment time , finding x and y position and move clock.

 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no state in that class, so there's no point in creating ClockProperties objects. I would just make all of the members static (and remove the setter methods which simply initialize variables to constant values).
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Paul.

I used all of the variable static because they are fixed and there value is not changed through the programme. You are right there is no requirement to use setter method. I edited my post. Removed all setter methods. Point taken.

I extend this class because i am using common methods. Do also need to make them static ?

Also any further suggestion...
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also please suggest on MyClock class pasted in first. I basically having doubt in createAndShowGUI and main method. As per oracle docs which i refereed earlier, createAndShowGUI method was static called from the event dispatch thread but in my class i made this method non static and called from an annoymous object because in this method i mentioned inner class non static paintComponent method. I am wondering is it good approach?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer an anonymous object myself. As you will see here.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks campbell..

I have another doubt.. Whether we need to make GUI creation and logic of clock movement into the separate thread or in single thread..

Like i make a GUI into EDT and call GUIRepaint method in main method itself which repaint the GUI and sleep for a sec. I was reading an article in which it is mentioned that " all codes accessing the GUI components should be run on the event-dispatching thread". I am feeling lost for multithreading in Swing/GUI. Any help greatly appreciated..
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:
Thanks campbell..
You're welcome
. . . Like i make a GUI into EDT and call GUIRepaint method in main method . . .
You are now running in two threads. Since the JPanel is not thread‑safe, things might go wrong. Core Java™ Vol I‑Fundamentals, by Cay Horstmann and Gary Cornell, page 909.

If you are scheduling things in Swing®, go through the Java tutorials and find about Timers.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Campbell.. I am currently reading Swing concurrency in multi threading environment and after that i will read about Timers.. I will get back to this post soon i finished these topics...
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been through some online tutorials and learnt some concurrency issue in SWING. To overcome it we need to define GUI in EDT and also for improving my code i have create one more thread inside EDT and that works fine. Now for repetitive work instead of Timer i used ScheduledExecutorService as suggested by following article. http://stackoverflow.com/questions/2959718/dynamic-clock-in-java

Now i want to do all task in a thread inside EDT, i want to start 3 thread each for second, minute and hour. Each of them represent by individual class. I can use pool of 3 threads in ScheduledExecutorService and scheduled each of class in it for 1 sec, 1 min and 1 hour interval. I tried to implement second clock first. I defined it with a single thread in pool and write run method which calculate at every second but GUI is not updating. I know am doing something wrong here to call from 'gui' variable but not sure how to correct it.

I have mentioned some code for the same.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:I have been through some online tutorials and learnt some concurrency issue in SWING.

I have to admit not having read the entire thread, but I'm not sure why concurrency would be a problem here. This is for a desktop surely, so there's little likelihood of you needing to worry about interference or race conditions since a GUI only has one Thread (unless things have changed since I was learning Swing).

One piece of general advice:
Separate your "clock" logic from your GUI completely. Right now they seem to be all mixed up. Get the thing working from the console first, and then plug it into a GUI.

I also don't like the instructions you've been given. They're hamstringing you. If I was designing a clock, I'd make it immutable to start with. That way, you don't have to worry about thread-safety at all. Once I got that working, then I'd add whatever synchronization was needed to "advance" it as required.

However you do it, my suspicion is that you should only have one update method; all the others should simply be "reads". That way, any synchronization you need (if any; and I'm not at all sure you do) is confined to a single method.

Winston
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Winston, i figured it out and also separate GUI and clock logic.

If I was designing a clock, I'd make it immutable to start with. That way, you don't have to worry about thread-safety at all.


What do you mean by this? Do you mean to make all the variable final ???
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:What do you mean by this? Do you mean to make all the variable final ???

Sure. Want to find out the "current" time? Create a new "clock".

Probably not the most space-efficient; but that's what the garbage collector was created for - and it allows you to test your object without worrying about all the distractions of Thread-safety and synchronization.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!