Page 1 of 2 12 LastLast
Results 1 to 20 of 33
Like Tree3Likes

Thread: How can I go about making propper classes for propper java coding?

  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default How can I go about making propper classes for propper java coding?

    The code at the bottom here is error free and does what I needed it to do for testing purposes. I am making a flash card program to study jazz more efficiently. This program will be expanded upon quit frequently: more options, more drills...

    When you load the code you will see a 5 columns of check boxes, 5 columns of combo boxes, 2 buttons and a display at the bottom.
    My code works, now it is times to use what I learned from this test to design a great program.

    The final project will have Graphics2D i am sure.


    SO HERE IS WHAT MY PROGRAM WILL DO
    ************************************************** ***
    1) Option screen with these buttons (Song Drill) (Custom Drill) (Random Drill);
    ***Lets start wiith the "Load Drill"
    2) Click Load Drill and it has a series of saved files to load.
    3) You also have the option on that screen to create new file.
    ***Create new file
    4)Create new file will load a screen like this...
    *** It will have all the ComboBoxes with an add button
    *** You will also be able to delete previous added
    5) when you are done adding chords hit save
    6) you can then load this file and it will randomize the added chords or play them in order depending on option selected.

    Custom drill is also going to have many options but there is going to be 12 or 13 tabs that have the same options over and over.

    So as you can see I am going to need to reuse those checkboxes a lot and they will need to apply to the different tabs.


    **********************************
    SO how do I design this with proper classes?
    **********************************


    Java Code:
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JComboBox;
    import javax.swing.JCheckBox;
    import javax.swing.JButton;
    import java.util.Random;
    import java.util.ArrayList;
    
    public class ChordRecallTest extends JFrame implements ActionListener {
    
    	private final String[] CHORD_SPACE = {"Open", "4", "X", "Y", "A", "B"};
    	private final String[] CHORD = {"Ab", "A", "Bb", "B", "C", "Db", "D", "Eb", "E", "F", "Gb", "G"};
    	private final String[] CHORD_QUALITY = {"M7", "m7", "7", "mM7"};
    	private final String[] ADD_TYPE = {"9", "13", "#5", "b5", "#9", "b9"};
    
        private ArrayList<String> chordSpaceHold = new ArrayList<String>();
        private ArrayList<String> chordHold = new ArrayList<String>();
        private ArrayList<String> chordQualityHold = new ArrayList<String>();
        private ArrayList<String> addTypeHold = new ArrayList<String>();
    
        private ArrayList<JCheckBox> chordBoxes = new ArrayList<JCheckBox>();
    	private ArrayList<JCheckBox> chordSpaceBoxes = new ArrayList<JCheckBox>();
    	private ArrayList<JCheckBox> chordQualityBoxes = new ArrayList<JCheckBox>();
    	private ArrayList<JCheckBox> addTypeBoxes = new ArrayList<JCheckBox>();
    
    	private String space, chord, quality, addType;
    
        private JComboBox chordComboBox, chordQualityComboBox, addTypeComboBox, chordSpaceComboBox;
        private JTextArea chordText;
        private JCheckBox addTypeCheck;
        private JButton addButton, randomButton;
        private JPanel chordSpacePanel, chordPanel, chordQualityPanel, addTypePanel, buttonPanel;
    
        private Random rand = new Random();
    
    	public static void main(String[] args) {
    		new ChordRecallTest().setVisible(true);
    	}
    
    	private ChordRecallTest(){
    		super("Chord Recall Test");
    		setSize(800, 600);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setLayout(new GridLayout(3, 1));
    
    		chordSpacePanel = new JPanel();
    		chordPanel = new JPanel();
    		chordQualityPanel = new JPanel();
    		addTypePanel = new JPanel();
    		buttonPanel = new JPanel();
    		JPanel p1 = new JPanel();
    		JPanel p2 = new JPanel();
    		JPanel p3 = new JPanel();
    
    		p1.setLayout(new GridLayout(1, 25));
    		p2.setLayout(new GridLayout(1, 5));
    		p3.setLayout(new GridLayout(2, 2));
    
    		chordText = new JTextArea(25, 1);
    		chordText.setEditable(false);
    
    		addButton = new JButton("Display Chord");
    		randomButton = new JButton("Random");
    		addButton.addActionListener(this);
    		randomButton.addActionListener(this);
    
    		createComboBoxes();
    		addCheckBoxesToPanels();
    
    		p1.add(chordSpacePanel);
    		p1.add(chordPanel);
    		p1.add(chordQualityPanel);
    		p1.add(addTypePanel);
    
    		p2.add(chordSpaceComboBox);
    		p2.add(chordComboBox);
    		p2.add(chordQualityComboBox);
    		p2.add(addTypeComboBox);
    
    		p3.add(addButton);
    		p3.add(randomButton);
    		p3.add(chordText);
    
    		add(p1);
    		add(p2);
    		add(p3);
    
    	}
    
    	private void recordSelectedBoxes() {
    
    		chordHold.clear();
    		chordQualityHold.clear();
    		chordSpaceHold.clear();
    		addTypeHold.clear();
    
    
    		if(chordSpaceHold.size() == 0) {
    			chordSpaceBoxes.get(0).setSelected(true);
    			chordSpaceHold.add(chordSpaceBoxes.get(0).getText());
    		}
    		if(chordHold.size() == 0) {
    			chordBoxes.get(0).setSelected(true);
    			chordHold.add(chordBoxes.get(0).getText());
    		}
    		if(chordQualityHold.size() == 0) {
    			chordQualityBoxes.get(0).setSelected(true);
    			chordQualityHold.add(chordQualityBoxes.get(0).getText());
    		}
    		if(addTypeHold.size() == 0) {
    			addTypeHold.add(" ");
    		}
    
    		for(int i = 0; i < chordSpaceBoxes.size(); i++) {
    			if(chordSpaceBoxes.get(i).isSelected()) {
    				chordSpaceHold.add(chordSpaceBoxes.get(i).getText());
    			}
    		}
    		for(int i = 0; i < chordBoxes.size(); i++) {
    			if(chordBoxes.get(i).isSelected()) {
    				chordHold.add(chordBoxes.get(i).getText());
    			}
    		}
    		for(int i = 0; i < chordQualityBoxes.size(); i++) {
    			if(chordQualityBoxes.get(i).isSelected()) {
    				chordQualityHold.add(chordQualityBoxes.get(i).getText());
    			}
    		}
    		for(int i = 0; i < addTypeBoxes.size(); i++) {
    			if(addTypeBoxes.get(i).isSelected()) {
    				addTypeHold.add(addTypeBoxes.get(i).getText());
    			}
    		}
    	}
    
    	private void createComboBoxes() {
    		chordSpaceComboBox = new JComboBox(CHORD_SPACE);
    		chordComboBox = new JComboBox(CHORD);
    		chordQualityComboBox = new JComboBox(CHORD_QUALITY);
    		addTypeComboBox = new JComboBox(ADD_TYPE);
    	}
    
    	private void addCheckBoxesToPanels() {
    
    		for(int i = 0; i < CHORD_SPACE.length; i++) {
    			chordSpaceBoxes.add(new JCheckBox(CHORD_SPACE[i], false));
    			chordSpacePanel.add(chordSpaceBoxes.get(i));
    		}
    		for(int i = 0; i < CHORD.length; i++) {
    			chordBoxes.add(new JCheckBox(CHORD[i], false));
    			chordPanel.add(chordBoxes.get(i));
    		}
    		for(int i = 0; i < CHORD_QUALITY.length; i++) {
    			chordQualityBoxes.add(new JCheckBox(CHORD_QUALITY[i], false));
    			chordQualityPanel.add(chordQualityBoxes.get(i));
    		}
    		for(int i = 0; i < ADD_TYPE.length; i++) {
    			addTypeBoxes.add(new JCheckBox(ADD_TYPE[i], false));
    			addTypePanel.add(addTypeBoxes.get(i));
    		}
    	}
    
    	private void printChord() {
    		System.out.println();
    		String test = space + " type: " + chord + quality + addType;
    		chordText.setText(test);
    		System.out.println(test);
    	}
    
    	private void displayChord() {
    		space = (String)chordSpaceComboBox.getSelectedItem();
    		chord = (String)chordComboBox.getSelectedItem();
    		quality = (String)chordComboBox.getSelectedItem();
    		addType = (String)addTypeComboBox.getSelectedItem();
    		printChord();
    
    	}
    
    	private void randomChord() {
    
    		recordSelectedBoxes();
    
    		int num;
    		num = (int)(Math.random()*chordSpaceHold.size());
    		space = (String)chordSpaceHold.get(num);
    		num = (int)(Math.random()*chordHold.size());
    		chord = (String)chordHold.get(num);
    		num = (int)(Math.random()*chordQualityHold.size());
    		quality = (String)chordQualityHold.get(num);
    		num = (int)(Math.random()*addTypeHold.size());
    		addType = (String)addTypeHold.get(num);
    		printChord();
    	}
    
    	public void actionPerformed(ActionEvent ae) {
    
    		if(ae.getActionCommand().equals("Display Chord")) {
    			displayChord();
    		}
    		if(ae.getActionCommand().equals("Random")) {
    			randomChord();
    		}
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How can I go about making propper classes for propper java coding?

    Look into using either the MVC (model-view-control) design pattern or one of its many variations which at its core, asks you to separate out your program logic from your GUI. The logic or model part should be coded in a way so that it can be used in a Swing GUI, or SWT GUI, or as a web application, all with little modification.

    So consider creating some enums for things that won't change, such as enum for the 12 note names, the octave, the different chord types, major, minor, dominant 7th, major 7th, minor 7th...

    Then consider creating classes that represent the nouns in your logic.

    Then give the classes methods that correspond to the verbs that are associated with the nouns...
    Last edited by Fubarable; 08-08-2012 at 05:22 PM.

  3. #3
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Re: How can I go about making propper classes for propper java coding?

    Looks like i need to learn what an enum is.

    Ok so just so you understand the program is going to record text as the chords.
    Its not going to have logic and actually understand what a b7th is just yet.
    Ill do that later. I need to get this working as quick as possible.

    So I need to learn about unums and threads right now. I will check out MVC.
    I also have Head First OO Design and Analysis book and have the Head First Design Patterns book waiting at the library for me.
    Will any of these books help?

    thanks.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How can I go about making propper classes for propper java coding?

    Quote Originally Posted by AcousticBruce View Post
    Looks like i need to learn what an enum is.
    It's a nice way of defining constants for your program -- a collection of objects that remain constant throughout the program's run. The classic example of this is using enums Rank and Suit for creating a playing Card class: enum tutorial


    Ok so just so you understand the program is going to record text as the chords.
    Its not going to have logic and actually understand what a b7th is just yet.
    Ill do that later. I need to get this working as quick as possible.
    We have competing requirements here:
    • creating a quick and dirty program that runs now, and
    • creating a program that is well designed and able to be more easily extended, modified and enhanced.


    Which is it going to be?

    So I need to learn about unums and threads right now. I will check out MVC.
    I also have Head First OO Design and Analysis book and have the Head First Design Patterns book waiting at the library for me.
    Will any of these books help?
    Yes, those books will help you immensely.

  5. #5
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Re: How can I go about making propper classes for propper java coding?

    About the books... I am going to do one at a time. So if you can be so kind to pick which one to do first. Ill just take back the other for now.

    Ok well then for now I need to make it just display text. I am not interested in creating music logic right now.
    Lets just say this is version 1 and in version 2 I will redo most of the code if needed to be able to have music logic and better code.

    Besides I need to learn how to structure classes and use patterns. But I want to be practicing with my flash card app bay Monday.

    Its just a learner program that is killing 2 birds with one stone right now.

    So I will focus on creating classes for what it does now, learn about proper coding while I am doing this. Then later focus on a version 2 which will be much more in depth.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How can I go about making propper classes for propper java coding?

    Quote Originally Posted by AcousticBruce View Post
    About the books... I am going to do one at a time. So if you can be so kind to pick which one to do first. Ill just take back the other for now.
    I've only gone through their design patterns book, but having said that, I think that OO design and analysis is a better place to start.

    Ok well then for now I need to make it just display text. I am not interested in creating music logic right now.
    Lets just say this is version 1 and in version 2 I will redo most of the code if needed to be able to have music logic and better code.
    I have to chuckle, because I've seen so many instances in my code and that of others where they say this, but then eventually build on the initial quick and dirty code. On the other hand, there are those who espouse a similar view towards software construction, using "tracer bullet design and prototyping" as a way of building your program. You can read up on it here: Tracer Bullets and Prototypes

    Besides I need to learn how to structure classes and use patterns. But I want to be practicing with my flash card app bay Monday.

    Its just a learner program that is killing 2 birds with one stone right now.

    So I will focus on creating classes for what it does now, learn about proper coding while I am doing this. Then later focus on a version 2 which will be much more in depth.
    OK, fair enough. Do you have any specific current spots where you're stuck, any specific questions?

  7. #7
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Re: How can I go about making propper classes for propper java coding?

    Quote Originally Posted by Fubarable View Post
    I have to chuckle, because I've seen so many instances in my code and that of others where they say this, but then eventually build on the initial quick and dirty code. ...
    I am the type of person that likes to learn a correct efficient way and I tend to start from scratch a lot. Funny thing is that starting from scratch tends to be inefficient LOL. But I will redo the code from scratch most likely in v2. Its been a year since I messed with java. Look at my post the last few days it was really basic and then BOOM now I am almost back to where I was a year ago . I type all the code and I do not use auto-complete or suggestions. This is what I am talking about cutting corners, which I am unlikely to do. I try to make the learning experience the most beneficial.

    Quote Originally Posted by Fubarable View Post
    OK, fair enough. Do you have any specific current spots where you're stuck, any specific questions?
    Yes I am unsure how to make the classes.

    *There is a screen that will be run of threads (i think.. i still need to learn what this is though) and it will display randomized or specific order of chords and refresh with a new one with a timer or a key press.

    There is an option menu at the beginning with at least 2 choices. The load/ create menu.

    Then of course lots of final instances.

    So I want to start out making the right classes. Maybe I can change my current code as-is and make it into proper methods and classes and I can use this to understand how to continue.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How can I go about making propper classes for propper java coding?

    Quote Originally Posted by AcousticBruce View Post
    Yes I am unsure how to make the classes.

    *There is a screen that will be run of threads (i think.. i still need to learn what this is though) and it will display randomized or specific order of chords and refresh with a new one with a timer or a key press.
    You'll still probably want to create a pure logic (non-GUI) class called Chord that will encapsulate the concept of a chord. You will likely want to create a collection of Chords, perhaps an ArrayList<Chord> and then in some event method, randomly select a Chord from this collection. You'll probably want to create a GUI representation for a chord (GuiChord?) which initially can be as simple as a String, but later may contain an Image of some sort, or perhaps better an ImageIcon that holds an Image. You can display your GuiChord in a JLabel, again perhaps initially as a String, but latter by setting the ImageIcon of the JLabel effectively swapping images.

    There is an option menu at the beginning with at least 2 choices. The load/ create menu.
    If loading from a disk or internet source, you'll want to create an IO class specifically for this, and you'll want to run it on a thread that is background to the Swing event thread.

  9. #9
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Re: How can I go about making propper classes for propper java coding?

    I am really trying here. It seems that I am at a complete wall. I have no idea even how to start. What class would be a good one to start?

    What type of code is in the class with the main method?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How can I go about making propper classes for propper java coding?

    Quote Originally Posted by AcousticBruce View Post
    I am really trying here. It seems that I am at a complete wall. I have no idea even how to start. What class would be a good one to start?
    I'd start with the most basic important class and work from there. This in my mind would be the chord class.

    What type of code is in the class with the main method?
    Since this is a Swing GUI program, the main method will:
    • Create a Runnable and queue it on the Swing event queue by calling SwingUtilities.invokeLater(Runnable) same as you see in the Swing tutorials
    • In this Runnable create the GUI and have it displayed.
    • If this were my GUI, I'd also create my Model and Control classes instances and make my connections between control and model and control and GUI.
    • The main method would not be large, perhaps 40 lines at most.


    Question: are you studying piano chords, guitar chords, basic abstract chord concepts or something completely different?

  11. #11
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Re: How can I go about making propper classes for propper java coding?

    Quote Originally Posted by Fubarable View Post
    I'd start with the most basic important class and work from there. This in my mind would be the chord class.
    YEs this does make sense. But what does this class do? Is it just program logic? Does it send to GUI? Does it just have a bunch of setters and getters?


    Quote Originally Posted by Fubarable View Post
    Since this is a Swing GUI program, the main method will:
    • Create a Runnable and queue it on the Swing event queue by calling SwingUtilities.invokeLater(Runnable) same as you see in the Swing tutorials
    • In this Runnable create the GUI and have it displayed.
    • If this were my GUI, I'd also create my Model and Control classes instances and make my connections between control and model and control and GUI.
    • The main method would not be large, perhaps 40 lines at most.
    Good information. Sounds like a great place to start with a tutorial. Where might this tutorial be?

    Quote Originally Posted by Fubarable View Post
    Question: are you studying piano chords, guitar chords, basic abstract chord concepts or something completely different?
    Chord voicings and ii V I's

    a '4' is a 1 3 5 7 stacked type bass in left
    a 'open' is a 1 5 - 7 3 type.
    a 'X' 1 7 - 3 13
    a 'Y' 1 3 - 7 9
    a 'A' is a rootless 7 9 3 13
    a 'B' is a rootless 3 13 7 9
    (13 is replaced with 5 for minor and major 7 types)

    I need to know the 7 and 3 for every single root there is ( well 12 with flat keys) this is ii V I practice.

    I want to get quick at these and select only a few to work on at a time.

    I want to save files like this

    name Misty
    and add certain chords in that song with specific voicings.
    Then start adding other voicings as time goes on.

    So piano mainly.


    So for now really no not too much logic just text. But I can see how ENUMS are helpful.

    enum question
    Its funny how these really smart people who make tutorials talk about this stuff like I know what it is.
    Like when they explain an enum like this.
    Well a enum is basically an enumeration of...
    What!? What the hell is an enumeration haha.

    I can kinda see how i might be able to use these. I checked out the card suit enum tutorial and I had no idea what the guy was talking about.

    So lets just talk about a ii V I enum.

    Bb = ii-C V-F I = Bb.
    So I can call it BFLAT {"C","F","Bb"}
    Something like that??
    Last edited by AcousticBruce; 08-10-2012 at 05:46 AM.

  12. #12
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: How can I go about making propper classes for propper java coding?

    Quote Originally Posted by AcousticBruce View Post
    Its funny how these really smart people who make tutorials talk about this stuff like I know what it is.
    Like when they explain an enum like this.
    Well a enum is basically an enumeration of...
    What!? What the hell is an enumeration haha.

    I can kinda see how i might be able to use these. I checked out the card suit enum tutorial and I had no idea what the guy was talking about.
    The problem of not knowing what a word means has been eradicated by Google.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  13. #13
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Re: How can I go about making propper classes for propper java coding?

    Quote Originally Posted by awinston View Post
    The problem of not knowing what a word means has been eradicated by Google.

    Whats Google?

  14. #14
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: How can I go about making propper classes for propper java coding?

    Quote Originally Posted by AcousticBruce View Post
    Whats Google?
    Google it.
    DarrylBurke likes this.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  15. #15
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Re: How can I go about making propper classes for propper java coding?

    Still confused to the enums and to why they are better than Static final. So I tried to make something useful.

    Lets say I want to put every single Root I want to study and have get methods for them (as seen in the code).

    It would be nice to to put something like the Major 7th of 'A' like this KEY.A.getM7(). Which would return a "G#"

    Would this be possible?


    Java Code:
    public class EnumTest {
        public static void main(String[] args) {
               
        }
    
    enum KEY {
    	Bb("Bb","C","D","Eb","F","G","A"),
    	C("C","D","E","F","G","A","B"),
    	D("D","E","F#","G","A","B","C#"),
    	A("A","B","C#","D","E","F#","G#"),
    	Eb("Eb","F","G","Ab","Bb","C","D");
    
    	private String R;
    	private String M2;
    	private String M3;
    	private String P4;
    	private String P5;
    	private String M6;
    	private String M7;
    
    	KEY(String a, String b, String c, String d, String e, String f, String g) {
    		R  = a;
    		M2 = b;
    		M3 = c;
    		P4 = d;
    		P5 = e;
    		M6 = f;
    		M7 = g;
    	}
    
    	public String getR() {
    		return R;
    	}
    	public String getM2() {
    		return M2;
    	}
    	public String getM3() {
    		return M3;
    	}
    	public String getP4() {
    		return P4;
    	}
    	public String getP5() {
    		return P5;
    	}
    	public String getM6() {
    		return M6;
    	}
    	public String getM7() {
    		return M7;
    	}
    }

  16. #16
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Re: How can I go about making propper classes for propper java coding?

    Well this is awesome. It worked! You do not even need to create a instance of an enum object. So a enum is basically static and final. Cool.
    Last edited by AcousticBruce; 08-11-2012 at 05:24 AM.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How can I go about making propper classes for propper java coding?

    Glad you got that sorted out, but fwiw I would approach this requirement from a different angle. I would create 12 enum constants for the 12 notes of an octave, and increment+modulo the ordinal of the enum constant to return another enum element at an appropriate interval. Something like
    Java Code:
    import java.util.EnumSet;
    
    public enum Note {
    
      A, Bb, B, C, Cs, D, Eb, E, F, Fs, G, Ab;
      static final EnumSet<Note> NOTES = EnumSet.allOf(Note.class);
    
      public Note getM3() {
        int index = (ordinal() + 4) % 12;
        for (Note note : NOTES) {
          if (note.ordinal() == index) {
            return note;
          }
        }
        return null;
      }
      
      // other methods
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Re: How can I go about making propper classes for propper java coding?

    Now that is really cool algorhythm!

    But can I get a Cm7#5 to display as C Eb G# Bb

    using gets like these?
    getB6() = Ab get#5() = G#

  19. #19
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Re: How can I go about making propper classes for propper java coding?

    Quote Originally Posted by AcousticBruce View Post
    Now that is really cool algorhythm!

    But can I get a Cm7#5 to display as C Eb G# Bb

    using gets like these?
    getB6() = Ab get#5() = G#

    Here is an Example....

    Java Code:
    public class EnumTest {
        public static void main(String[] args) {
    		System.out.printf("A Cm7#5 contains: %s %s %s %s", Key.C.getR(), Key.C.getB3(), 
                 Key.C.getS5(), Key.C.getB7());
    	}
    }
    
    enum Key {
    	//  R    b2     2   #9     b3    3    4    #4    b5    5    #5    b6    6    b7    7
    	C( "C", "Db", "D", "D#", "Eb", "E", "F", "F#", "Gb", "G", "G#", "Ab", "A", "Bb", "B");
    
    	private String R;
    	private String B2;
    	private String M2;
    	private String S9;
    	private String B3;
    	private String M3;
    	private String P4;
    	private String S4;
    	private String B5;
    	private String P5;
    	private String S5;
    	private String B6;
    	private String M6;
    	private String B7;
    	private String M7;
    
    	Key(String r, String b2, String m2, String s9, String b3, String m3, String p4, String s4, String b5,
    	 String p5, String s5, String b6, String m6, String b7, String m7) {
    		this.R = r;
    		this.M2 = m2;
    		this.S9 = s9;
    		this.B3 = b3;
    		this.M3 = m3;
    		this.P4 = p4;
    		this.S4 = s4;
    		this.B5 = b5;
    		this.P5 = p5;
    		this.S5 = s5;
    		this.B6 = b6;
    		this.M6 = m6;
    		this.B7 = b7;
    		this.M7 = m7;
    	}
    
    	public String getR() {
    		return R;
    	}
    	public String getB2() {
    		return B2;
    	}
    	public String getM2() {
    		return M2;
    	}
    	public String getS9() {
    		return S9;
    	}
    	public String getB3() {
    		return B3;
    	}
    	public String getM3() {
    		return M3;
    	}
    	public String getP4() {
    		return P4;
    	}
    	public String getS4() {
    		return S4;
    	}
    	public String getB5() {
    		return B5;
    	}
    	public String getP5() {
    		return P5;
    	}
    	public String getS5() {
    		return S5;
    	}
    	public String getB6() {
    		return B6;
    	}
    	public String getM6() {
    		return M6;
    	}
    	public String getB7() {
    		return B7;
    	}
    	public String getM7() {
    		return M7;
    	}
    }

    Outputs this...
    Java Code:
    A Cm7#5 contains: C Eb G# Bb
    Last edited by AcousticBruce; 08-11-2012 at 08:54 AM.

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How can I go about making propper classes for propper java coding?

    Here's a further example of what I'm driving at, with a demo main(...) method.

    A chord is really a set of notes, and obtaining it as such lends the code the flexibility for future interaction with GUI components, something a mere String wouldn't so representatively afford.
    Java Code:
    import java.util.EnumSet;
    import java.util.SortedSet;
    import java.util.TreeSet;
    
    public enum Note {
    
      A, Bb, B, C, Cs, D, Eb, E, F, Fs, G, Ab;
      static final EnumSet<Note> NOTES = EnumSet.allOf(Note.class);
    
      public Note get0() {
        return this;
      }
    
      public Note getM3() {
        return getNote(4);
      }
    
      public Note getM5() {
        return getNote(7);
      }
    
      // other intervals
      
      private Note getNote(int interval) {
        int index = (ordinal() + interval) % 12;
        for (Note note : NOTES) {
          if (note.ordinal() == index) {
            return note;
          }
        }
        return null;
      }
    
      public SortedSet<Note> getMajorChord() {
        return getChord(0, 4, 7);
      }
      
      // other chords
    
      private SortedSet<Note> getChord(int... intervals) {
        SortedSet<Note> chord = new TreeSet<Note>();
        for (int interval : intervals) {
          chord.add(getNote(interval));
        }
        return chord;
      }
    
      @Override
      public String toString() {
        return super.toString().replace("s", "#");
      }
    
      public static void main(String[] args) {
        System.out.println(Note.A.getMajorChord());
      }
    }
    Is there really a minor 7th augmented 5th chord? Looks terribly discordant to me, but the only instrument I have at present is a harmonica, so short of writing code to generate it I can't hear what it would sound like.

    db
    Last edited by DarrylBurke; 08-11-2012 at 02:55 PM. Reason: Added toString() override
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 12
    Last Post: 12-12-2010, 06:04 PM
  2. Breaking out of Breakout! making new classes
    By sonny in forum New To Java
    Replies: 21
    Last Post: 05-05-2010, 03:29 AM
  3. Replies: 1
    Last Post: 08-11-2009, 02:00 PM
  4. Making A Set Of Classes "Importable"
    By JDCAce in forum Advanced Java
    Replies: 4
    Last Post: 12-05-2008, 09:11 AM
  5. Need help with Java classes for making a program.
    By TheDarkReverend in forum Advanced Java
    Replies: 2
    Last Post: 11-28-2008, 04:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •