Wednesday, June 27, 2012

Still Counts in Base 2

So one week done with the Java CS 55 course. We've reviewed some things that I never understood which are doubles. Double in Java is a decimal number. But when you add two doubles, you usually get a formatted number like such: 2.00 + 3.00 = 5.0000004

I wasn't sure why but I did some digging. Double is where the computer assigns the decimals as though in base 2, so 2^-6th would be something like a fractional decimal to count in which gives us the 0.00004 at the end of a double arithmetic. So it was odd when I'd arrive with a simple program adding two double numbers and get a sum. Thankfully in Java there's a class called DecimalFormat in the java.text.* package which allows me to force a specific format of the decimals as a method.

Like so:

public static void main(String[] args)
{
DecimalFormat f= new DecimalFormat("0.000");
String numStr1 = JOptionPane.showInputDialog("Enter an Integer");
double n1 = Double.parseDouble(numStr1);
String numStr2 = JOptionPane.showInputDialog("Enter an Integer");
double n2 = Double.parseDouble(numStr2);
double total = n1+n2;
System.out.println("Total is "+total);
System.out.printf("Total is %6.2f\n", (n1+n2));
System.out.println("Total is "+f.format((n1+n2)));
System.exit(0);

This will give you three outputs, one is the unformatted normalized total, second is a float like total printed (something in C you can do all the time), and finally the formatted output.

I guess that also includes the code of the post. Till next time :)

Thursday, June 21, 2012

So the First Two Years Don't Count?

Well. It seems like it's been an eternity since I started this blog, let alone the countless attempts at continuing it. While my mom is long gone (4 years in July), my journey through college hasn't ended. Not the slightest.

Last post was done in 2009. Lets get back up to speed. I'm no longer a Chemical Engineering Major. Nay. I was enjoying the labs much more, however, to do anything NEW I would have to redesign the experiments. That takes too much time. I got bored of memorizing these interesting bits and pieces of chemistry, even Organic Chemistry was fun to an extent. I got tired of it though. I didn't see myself doing what I was doing in the lab every day.

What did I do? I did the worst thing you could do in college, switch majors in an entirely different field: Computer Science. Why Computer Science? My dad is a computer scientist at JPL (Jet Propulsion Laboratory for those who don't know the acronym), he's been working there as long as I've been living on Earth. He told me to take the intro CS class at SMC. I did last Fall. It was pretty boring as I knew pretty much 80% of the material covered in the course before stepping in the door. I continued with it because the introductory course is always boring or severely limited in what depth the materials are taught.

Winter 2012, CS 50 C Programming. All I can say is this: Where were you in high school?! I didn't imagine that something so difficult for others to understand to be blatantly easy for me to understand. I mean who wouldn't want to make things easier in terms of repeating the same thing over and over but with different inputs by writing a simple method to do everything being done? Make a new header file with methods to be called upon and you're pretty much set! This class was very short. 6 weeks short and I had difficulties in getting my mind to class. I endured it though.

Today, June 21, 2012 - I'm retaking my Java CS 55 course because I don't accept B's anymore. Although I did receive a B in my 3rd semester of Japanese (Reviewing for the Fall semester as I'm continuing on to Japanese 4), I can't say that I worked my butt off to learn everything well and do the best I could. Although this is technically not a class I'm registered in, I'm sitting in the Java class to keep my mind on things and not melt away as almost everyone does during the summer.

So Java, a language released in 1996 by Sun Microsystems (acquired by Oracle in 2009), is the first Object Oriented Programming language that I'm learning. What was the first language you ask? Technically it was AppleScript. I used to get away with playing games on my PowerMac G5 by writing scripts to disable various processes or programs that my dad installed to prevent me playing said games. Of course though, whichever script (the applications my dad used were most likely scripts) he used ended up being terminated faster than the script could start up and read the list of applications to be  prevented from opening. Reasoning behind this was that my script was about 9 lines long, it's really fast to execute, no end, and the processor never had to do anything but check that the process called "whateverapplicationpreventornamedhere" was forced to quit.

My dad was annoyed but impressed at the same time, the applications couldn't keep up with my scripts and there was no way to just prevent the script from activating faster than the application could catch it.

I digress. So today I'm writing yet another attempt to reboot a blog. I find it relieving to write these thoughts out. And possibly to keep a log of my journey through the forever evolving environment of Computer Science.

Program of the day:

public class Exercise6 {

public static void main(String[] args)
{
System.out.println("Plesae enter a number between 0 and 1000");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
int lastDigit = number % 10;
int remainingNumber = number / 10;
int secondLastDigit = remainingNumber % 10;
remainingNumber = remainingNumber / 10;
int thirdLastDigit = remainingNumber % 10;
int sum = lastDigit + secondLastDigit + thirdLastDigit;
System.out.println("The sum of all digits in "+number+" is "+sum);
}
}