Hello World, C# style

Advertisements

Hello everybody,

Michael here, and today I thought I’d introduce a new programming language to this wonderful blog-C#. This will be the ninth new programming tool I’ve introduced on this blog, after:

  • R
  • MySQL
  • Java
  • Python
  • GitHub
  • HTML
  • CSS
  • Bootstrap

Yea, we’ve covered a lot of programming in the last 6-and-a-half years, but hey, nothing wrong with picking up a new language, am I right?

With that said, let’s introduce ourselves to C#

What is C#?

C# is a general-purpose, object-oriented (much like Python and Java) programming language developed by Microsoft in 2000 that runs on a little something called the .NET framework.

What is the .NET framework? The .NET framework is Microsoft-developed open-source software framework that acts as a runtime environment for C# (.NET also works with other languages too like PowerShell and F#).

Setting up C#

Now, how do we set up an IDE to play around in the C# sandbox? Since C# is a Microsoft-developed application, I’d suggest using a Microsoft-developed IDE for our C# development journey-Visual Studio. Here’s the link to download it; follow all necessary instructions to install the IDE-https://code.visualstudio.com/. Also, unless you want to develop C# applications for a giant (or even small) business, download the Community version of Visual Studio, as that IDE is free to use.

Hello world, C# style

Once you’ve installed the IDE, let’s go play around with it by doing the most cliche of introductory programming tasks-learning to print Hello world (yea I know hello world is a bit overrated for programming language introductions, but I think it works, so I’m gonna use it).

Now, once we’ve installed Visual Studio, let’s set up our new C# project.

In the Visual Studio interface, create a new project. You should see an interface that looks like this:

In the search bar at the top, type in console app and scroll down until you see something that looks like Console App (.NET Framework). Most importantly, remember to select the Console App option with the green C# icon, as we are working with C# (as you can see from this screenshot, you can also build Visual Basic and F# applications).

Click Next, which will take you to this screen:

All you really have to do here is give your project a name (I used MichaelsSandbox here) and click Next, which will take you to the following screen:

Assuming these are the default options, leave them as they are and click Create to create your first C# project. Also, copy this code onto the newly generated file:

using System;

namespace HelloWorld
{
    class MichaelsSandbox
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

After you’ve copied the code onto the new project file, save the file and click on the green-outlined triangle icon to run the script.

Interestingly, the output won’t show on the interface itself but rather on a separate command prompt. Granted, all the code really did was print Hello World! on a command prompt, but that was the whole point-to give you a very very basic introduction to the world of C#.

  • Don’t mind the process ... exited with code 0 message-it’s standard to see it when running your C# programs (though you can turn off this message)

The script, explained

Now, since this is my first C# lesson, let’s explain the script, shall we?

  • using System-this indicates that the script will use the System namespace. In C#, a namespace is a means of organizing your code into containers, which contain various classes and methods. The System namespace acts as the runtime environment for C#, as it contains various fundamental components for C# to run like fundamental data types and input/output mechanisms.
  • namespace HelloWorld-as I mentioned earlier, a namespace is a means of organizing your code into containers with their own classes and methods. In this script, my code will be in the HelloWorld “container”
  • class MichaelsSandbox-Much like in Java and Python, a C# class holds data and methods for a certain aspect of a program’s functionality
  • static void main (String[] args)-This is C#’s version of the main method, much like Java’s public static void main (String[] args) or Python’s def main():-interestingly, Java’s and C#’s main methods are nearly identicial
  • Console.WriteLine("Hello World");-This line prints the specific text onto the console/IDE, much like Java’s System.out.println() method or Python’s print() method.
  • Two things to note about the Console.WriteLine() method-you always need a semicolon at the end of the method call (much like in Java) and you always need to wrap the text value to be printed in double quotes (also much like Java).

Also, here’s the script in my GitHub-https://github.com/mfletcher2021/blogcode/blob/main/IntroToCSharp.cs.

Thanks for reading,

Michael

Java Lesson 1: Introduction to Java & “Hello World”

Advertisements

Hello everybody,

This is Michael, and first of all, Happy New Year! Can’t believe it’s already 2019! I hope you all had a wonderful holiday season. I also wanted to say thank you for reading my posts in 2018, and I hope you learned some new skills along the way. Get ready for some exciting programming and analytical content in 2019!

Now, since is the first post of 2019, I thought I’d try discussing something new-Java. Some of you might be wondering “What is Java?” Java is basically a general-purpose computing language that is used for Android apps (whether on the phone or tablet), server apps for financial services companies (think large hedge funds like Goldman Sachs), software tools (like NetBeans-more on that later), and games like Minecraft.

One thing to keep in mind with Java is that is more for building applications and programs. Java isn’t really useful for statistical analyses like R is, or database building/querying like MySQL.

Now to understand Java, I will discuss a concept called IDEs, or Integrated Development Environments. IDEs are basically the software systems you will use to write, test, and debug your programs. My personal favorite IDE is NetBeans, which is what I will use for all of my Java posts (there are other IDEs to choose from like JCreator, which I’ve used but didn’t like as much as NetBeans). NetBeans, and most other Java IDEs, are free to download; another perk is that they will point out and explain errors in your code (whether syntax or otherwise) by displaying a red line underneath the line(s) of code with the error(s).  You might also see yellow lines underneath certain portions of code, but that is usually displayed to suggest how to make your code neater rather than to point out errors in your code.

So let’s get started and open up NetBeans:

This screen is what you will see every time you open up NetBeans. The other tabs contain other Java projects I’ve done in my spare time.

How do we create a new Java project? Here’s a video showing how to create a new Java project-Java Lesson 1.

As you can see, you must first create a project, then create a class. You can have several classes in your project, but for now let’s stick with a single main class.

Once you finish creating your project and class, you should see your interface, which looks like this:

The interface is where the magic of Java programming happens; here, you write, run, and debug the code you will use for your program.

Now let’s demonstrate the programming process using the most famous example for beginner coders-the “Hello World” program. Here’s a video demonstration for anyone interested-Hello World demo.

Some of you are probably wondering “What does all this code mean?” Let me explain.

  • A package is a mechanism for grouping classes. The package javalessons gets its name from our main class-JavaLessons. If we had several packages in our project, then they would all belong to the javalessons package. 
    • There are two types of packages-user defined (like the javalessons package) and built-in (which come with Java). Some built-in packages include:
      • java.util– contains utility classes which implement data structures such as Dictionary; commonly used for Date/Time operations
      • java.awt– commonly used for graphical interfaces purposes (such as creating buttons or drop-down menus)
  • Two concepts that you should know are objects and classes, which are the two most fundamental concepts in Java.
    • Java is an object-oriented programming language, which means that Java programs consist of objects that can either act alone or interact with one another.
      • Think of it this way-the world is full of objects (people, trees, dogs, food, electronics, houses, etc.). Each of these objects can perform several actions, and each action affects some of the other objects in the world. For instance, people buy and eat food, while dogs pee on trees.
      • Simple enough, right? Let’s say we had a program that analyzes the inflow and outflow of animals at an animal shelter. The program would have an object to represent each dog or cat that enters or exits the shelter as well as objects for cages, people that surrender/adopt animals, and so on.
      • Every object has characteristics, or attributes. Using the aforementioned shelter example, animals have names, ages, genders, breeds, medical conditions, and so on.
      • The values of an object’s attributes give the object a state. For instance, a dog can be named Spots, be 10 years old, male, and a Chocolate Labrador. The things an object can do are its behaviors; using the example I just mentioned, dogs can bark, growl, roll over, eat, sleep, poop , and so on.
      • In Java, each object’s behavior is defined by a method (more on that later)
    • Similar objects of similar data types belong to the same class, which is essentially a blueprint for creating objects. Using the dog example, all Chocolate Labradors that enter or exit the shelter would belong to the class ChocolateLabrador.
  • You’ll notice that the words package, public class, and public static void are all in blue. That’s because they are reserved words, which means you can’t use them as the names of objects or variables because these words are already reserved for the syntax of Java programming. Here’s a handy glossary of reserved words-Reserved Words in Java.
  • One of the first methods programmers learn is the public static void main (String [] args) method; this method is essentially the entry point for Java programs. This method contains the block of code that will perform a certain action-in this case, display our output. Let’s break it down word by word:
    • public-meaning any object (whether in the same class or different classes) can use the main method
    • static-means the method belongs to its class and not to any particular object
    • void-meaning the method doesn’t return any value
    • main-this is just the name of the method
    • String [] args-meaning the necessary parameters for this method are arrays of Strings; arrays are just linear arrangements of things. I’ll cover arrays more in a future lesson.
  • public class means the class can be called from anywhere. For instance, if you were trying to create a game with several different character classes, then provided that your game class is public, the class (and any public methods) can be called by any of the character classes.
  • System.out.println(string) is one of the first lines of code beginner Java programmers learn. This line of code prints out whatever is in the parentheses (which is usually a string). In this case, we’ll be printing out “Hello World” (I know it’s cliche, but it’s a great first Java lesson). Now let’s break down this line of code word-by-word:
    • System-a built-in class that contains useful methods and varaibles
    • out-a static variable in the System class that represents the output of your program
    • println-the method that prints a line of code

Now, before I go, I want to mention a little caveat when it comes to the println method-the difference between it and the print method. See, since println and print are so similarly named, you might think you can use the two methods interchangeably and get the same result. Not true-check out the pictures below for proof.

Granted, I’m trying to do the same thing in both examples (print “Hello World” twice). But in the first picture, since I used println both times, the two instances of “Hello World” are displayed one on top of the other. In the second picture, where I used print both times, the two instances of “Hello World” are displayed right next to each other without a space.

Thanks for reading, and here’s to expanding our analytical and programming knowledge in 2019!

Michael