Please follow me here!

Thursday, May 30, 2013

JAVA Program: Object-Oriented Programming Class, the PopMusicClass

Hello, everyone.

I want to share with you something new out of my knowledge of JAVA Programming; a fresh, original class I've designed where everything's quite simple, but is new to people who are yet unaware of the object-oriented programming approach.

Basically, the purpose of this approach is to create programs and methods that can be reused by any other programs developed in the same programming language. In fact, this approach was done to design the original JAVA classes that most JAVA programmers experience every time we write something. String, Short, Integer, File, Exception, Scanner, JLabel; all of the net classes that exist in today's versions of JAVA SDKs.

Here, what I have designed here is a class called PopMusicClass where it is designed to hold data of pop culture songs, especially those made today. It's not Beethoven or Scott Joplin's music. It's rather the music that we are making today.

I'll show you the code first, then tell you what it consists of.


public final class PopMusicClass
{
private String nameOfSong;
private String artist;
private String nameOfAlbum;

private short yearOfRelease;
private String releaseDate;

private String recordingStudio;
private String mixingStudio;

private String writers;
private String publishingRecord;
private String distributionStudio;

private String format;

public static void main(String[] args) // Testing only
{
PopMusicClass object1 = new PopMusicClass();
PopMusicClass object2 = new PopMusicClass("Firework", "Katy Perry", "Teenage Dream", (short)2010, "October 26, 2010",
"Roc the Mic Studios", "Roc the Mic", "Perry, Mikkel S. Eriksen, Tor Erik Hermansen, Sandy Wilhelm, Ester Dean",
"Capitol Records", "N/A", "CD, Downloadable form");

System.out.println(object1 + "\n\n" +  object2);
}

public PopMusicClass()
{
nameOfSong = null;
artist = null;
nameOfAlbum = null;

yearOfRelease = 1930;
releaseDate = null;

recordingStudio = null;
mixingStudio = null;

writers = null;
publishingRecord = null;
distributionStudio = null;
format = null;
}

public PopMusicClass(String nameOfSong, String artist, String nameOfAlbum, short yearOfRelease, String releaseDate, 
String recordingStudio, String mixingStudio, String writers, String publishingRecord, String distributionStudio,
String format)
{
this.nameOfSong = nameOfSong;
this.artist = artist;
this.nameOfAlbum = nameOfAlbum;

this.yearOfRelease = yearOfRelease;
this.releaseDate = releaseDate;

this.recordingStudio = recordingStudio;
this.mixingStudio = mixingStudio;

this.writers = writers;
this.publishingRecord = publishingRecord;
this.distributionStudio = distributionStudio;
this.format = format;
}

public void setNameOfSong(String nameOfSong)
{
this.nameOfSong = nameOfSong;
}

public void setArtist(String artist)
{
this.artist = artist;
}

public void setNameOfAlbum(String nameOfAlbum)
{
this.nameOfAlbum = nameOfAlbum;
}

public void setYearOfRelease(short yearOfRelease)
{
this.yearOfRelease = yearOfRelease;
}

public void setReleaseDate(String releaseDate)
{
this.releaseDate = releaseDate;
}

public void setRecordingStudio(String recordingStudio)
{
this.recordingStudio = recordingStudio;
}

public void setMixingStudio(String mixingStudio)
{
this.mixingStudio = mixingStudio;
}

public void setWriters(String writers)
{
this.writers = writers;
}

public void setPublishingRecord(String publishingRecord)
{
this.publishingRecord = publishingRecord;
}

public void setDistributionStudio(String distributionStudio)
{
this.distributionStudio = distributionStudio;
}

public void setFormat(String format)
{
this.format = format;
}

public String getNameOfSong()
{
return nameOfSong;
}

public String getArtist()
{
return artist;
}

public String getNameOfAlbum()
{
return nameOfAlbum;
}

public short getYearOfRelease()
{
return yearOfRelease;
}

public String getReleaseDate()
{
return releaseDate;
}

public String getRecordingStudio()
{
return recordingStudio;
}

public String getMixingStudio()
{
return mixingStudio;
}

public String getWriters()
{
return writers;
}

public String getPublishingRecord()
{
return publishingRecord;
}

public String getDistributionStudio()
{
return distributionStudio;
}

public String getFormat()
{
return format;
}

public boolean equals(Object o)
{
if (o instanceof PopMusicClass)
{
PopMusicClass object = ((PopMusicClass)o);

return this.nameOfSong.equals(object.nameOfSong) && this.artist.equals(object.artist) &&
this.nameOfAlbum.equals(object.nameOfAlbum) && this.yearOfRelease == object.yearOfRelease &&
this.releaseDate.equals(object.releaseDate) && this.recordingStudio.equals(object.recordingStudio) &&
this.mixingStudio.equals(object.mixingStudio) && this.writers.equals(object.writers) &&
this.publishingRecord.equals(object.publishingRecord) && 
this.distributionStudio.equals(object.distributionStudio) &&
this.format.equals(object.format);
}
else
return false;
}

public String toString()
{
return nameOfSong + "\nArtist: " + artist + "\n\nName of Album: " + nameOfAlbum + "\n\nYear of Release: " + yearOfRelease + 
"\nRelease Date: " + releaseDate + "\n\nRecording Studio: " + recordingStudio + "\nMixing Studio: " + mixingStudio + 
"\nWriters: " + writers + "\n\nPublishing Record: " + publishingRecord + "\n\nDistribution Studio: " + distributionStudio +
"\nFormat: " + format;
}
}

The program consists of  11 fields, two constructors, 11 set methods, 11 get methods, and two methods that override those with the same name and signature from the Object class: equals and toString. Oh, and the final modifier in the header of the class definition is to tell the compiler that this class cannot be inherited.

The main method you see at the top of the code is to test with constructing and displaying them on the console. The other methods were not tested for validation.

For those of you who don't know what Object-Oriented Programming is, it's simply a term to describe reusable programs and data structures that the only way you can modify data and prevent so many bugs happening in your program is to call in the class methods defined in the object you create. This is to prevent accidental corruption that can bug your programs and cause multiple problems.

How it works is that the data that each object carries is encapsulated in such a way that the user has no way of gaining access to that data directly; the only way to access it is indirectly through the methods that operate on it. That's why the data fields are declared with the access specifier "private." When it comes time to program something else other than this class, it's important to ensure that we do not actually call in one of the object's fields and assign it with a new value or reference to an object.

As for the methods, because the user or programmer is able to call in those methods in a different program, they are declared with either "default", "protected:, or even "public" access modifier.

Anyway, I apologize to say, but I'm kind of lazy to keep on talking about this. I think I'll leave other explanations for another time, so I'll see you later!

Ciao!

Google+ Followers

Popular Posts by Gregory