Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Monday, 1 June 2020

Java programming and OOP

Either to revise or as additional sources, the followins resources may help you with Java programming and OOP.

 Hope they will be useful to you!

Tuesday, 3 December 2019

Lexicographical Order

This refers to the way strings and characters are alphabetised and compared.

In Java, you need to understand lexicographical order to properly implement (and debug) sorting algorithms.

The page below has the best, most concise and practical explanation I have found so far:

Monday, 31 December 2018

Warm up to Computer Science

These resources will help you practice and strengthen your Computer Science and practical programming skills. Multiple links are provided to cover a wider range of backgrounds, experience and needs.

Python is a great first programming language to learn for beginners, and Java is second to none to learn OOP and intermediate to advanced programming concepts. Java is a must for computer science students, and is also the prescribed programming language for the IB Diploma Programme Computer Science course with the Object Oriented Programming (OOP) option.

Good luck!

Theory/General Knowledge:
Practical aspect: Programming

Free eBooks recommended:
A few of the best reference sites about Java programming are

Monday, 20 August 2018

Setting up Java and Geany

In order to program in Java, you may want to follow the instructions and videos that I have prepared. They are simle and straightforward, and have no audio to keep the video lean and to prioritise video quality.

Videos show tutorials for both Windows 10 and macOS.

Setting up your Java programming tools, in order (JDK first, then Geany)
  1. Download and install Java Standard Edition Development Kit, version 8. You will have to agree to using their cookies and accept their License Agreement, then download the right JDK for your system (most likely Mac OS X x64 or Windows x64). Link: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
  2. Now download and install Geany from https://www.geany.org/download/releases. You will need to choose between Windows binaries (e.g. "geany-1.33_setup.exe - Full Installer including GTK 2.24 runtime") and macOS X binaries (e.g. "geany-1.33_osx.dmg - Geany and Geany-Plugins combined").
The following screen casts may help you:

Wednesday, 4 October 2017

UML Class Diagrams in a Nutshell

...A really small nutshell.

This webpage contains a nice, short and sweet explanation and side-be-side comparison between a class diagram and its corresponding Java code. Priceless.

Source: http://pages.cs.wisc.edu/~hasti/cs302/examples/UMLdiagram.html

Sunday, 23 April 2017

Arrays and Java

Just a short post with pretty much all you need to know about arrays in Java:
Below are some additional links about arrays and 2D arrays:

Tuesday, 20 September 2016

Netbeans and ArrayList

You may want to download and install Netbeans (the use of this IDE is OPTIONAL, but recommended for our IA as it makes life easier. If you are comfortable with Geany, stick to it. You may want to give Netbeans a try, though.

Our last data structure (for SL) is the Java built-in ArrayList:


Tuesday, 10 May 2016

Linked Lists

A couple of good links for revising singly and doubly linked lists:
The concepts, use, functionality, advantages and disadvantages are more important than the code when studying ADTs at an introductory level; focus on the diagrams rather than the Java (or code) implementations shown in these online resources.

Monday, 14 March 2016

Static vs Non-static in Java

A really good (and tough!) distinction that everyone coding in Java needs to know: the difference between static and non-static variables in Java.

In my opinion, tutorial4us did a great job explaining it. A very good read indeed.

My understanding is that instance (non-static) variables exist in each object (instance of the class). Each object will have its own value/content for each instance variable. Think of the name in a Person class. We define the name as an attribute or property of a person, and each Person object will have its own name.

Static variables exist only inside the class where they are declared. Some call them "class variables" because they are bound to the class, not the object. They are unique and when a new object is instantiated, the object does not get its own copy of it. Rather than using the dot notation with the object (object.nnnn), you would use it with the class (Class.nnnn).

Note that this also applies to, and is also often used with methods!

Think about these example:


public class Test
{
  public static void main (String args[])
  {
    int[] numbers = {1,2,3,4,5};
    System.out.println( numbers.length ); // =5
    // length (size/number of elements) of the array object
    System.out.println( Math.PI );
    // PI is stored as a static constant inside the Math class
    // we don't need to instantiate a Math object to use PI
  }
}

Thursday, 3 March 2016

this Java keyword

A feature of Java that usually confuses beginners is the "this" prefix.

In a nutshell, the this keyword is used to distinguish between parameters of a method and attributes (instance variables) of a class.

The following link will lead you to a web page that does a great job contrasting and explaining this with examples:

Sunday, 7 February 2016

Distribute your Java classes in a JAR file

The source of the information given in this post is http://www.skylit.com/javamethods/faqs/createjar.html
In my opinion, the simplest and quickest way to do this is using the command line.


Steps
  1. Start the Command Prompt. The easiest way is to Shift-Right Click on an empty spot in your Windows (File) Explorer. Make sure no files are selected, though. The idea is that your Windows Explorer should be in the location of your Java files.
  2. Navigate to the folder that holds your class files:
    Copy the path from Windows Explorer's address bar (more details about WE here), then type the following into the command prompt window:

    cd /d 
  3. minding the space at the end, then paste the path that you copied from Windows Explorer's address bar. Press Enter.
  4. If you haven't set your path to include JDK’s binaries (compiler and utilities, including the one that creates jar files), you need to do so by adding it to your default path (your folder may differ depending on the version of the JDK that you installed)
    path %path%;C:\Program Files\Java\jdk1.8.0_83\bin
  5. Compile your class(es):
    javac *.java
  6. Create a manifest file and your jar file (Replace IA with the relevant name to your project):
    echo Main-Class: IA > manifest.txt
    jar cvfm IA.jar manifest.txt *.class
    ---- or ----
    jar cvfe IA.jar IA *.class
  7. Test your jar:
    java -jar IA.jar
  8. Create a batch file so your end user can double click on it to run your command line application (you may also use Notepad to do this):
    copy con run.bat    (creates the file)
    java -jar IA.jar    (Press CONTROL Z and Enter to finish and save the file)
  9. Double click on run.bat to test it.

Tuesday, 22 December 2015

Java Scanner class confusion

If you have used Java's Scanner class, yopu may have come across an odd behaviour as you use the same Scanner object to input numbers and Strings.
I recently found the best explanation for this behaviour (it's not a bug), which you may find and here here at Daniweb. The best replies are from and


The reason it gives you trouble is because when the user enters an integer then hits enter, two things have just been entered - the integer and a "newline" which is \n. The method you are calling, "nextInt", only reads in the integer, which leaves the newline in the input stream. But calling nextLine() does read in newlines, which is why you had to call nextLine() before your code would work. You could have also called next(), which would also have read in the newline. (BestJewSinceJC)

Just change the delimiter.

Scanner in = new Scanner(System.in);
in.useDelimiter("\n");
(cgeier)

Hope you find these useful.

Friday, 23 December 2011

Sun Java on Ubuntu 11.10

Decided to 'fork' my previous post because since Ubuntu 11.10, Sun/Oracle Java has been removed from Canonical's partner repository. Fortunately, Roberto Ferramosca is providing the appropriate packages in his personal package archive (PPA).

So open your terminal, and let's go:

sudo add-apt-repository ppa:ferramroberto/java
sudo apt-get update
sudo apt-get install sun-java6-jdk sun-java6-jre sun-java6-plugin sun-java6-fonts

And that's it. Hope it's helpful. Now if you want to ensure Sun's JRE & JDK are the default, type in the terminal:
sudo update-alternatives --config java
and select your preferred alternative from the list. To select a Java compiler, type
sudo update-alternatives --config javac
Now on to the testing. You should get the version numbers of the currently installed and default runtime and compiler by typing (in the terminal):
java -version
javac -version

Sunday, 4 September 2011

Windows, Java and Geany

One of these things that happen in the worst possible moment...

If you want to use the nice and simple Geany IDE under Windows, I'd recommend you to install all the programming languages before you install Geany.

In my case, installing Java and then Geany didn't work at all. Geany wasn't able to compile or execute my Java code. Even after uninstalling and reinstalling both Geany and Java in all possible combinations.

Hope this warning will help someone out there.

Note: Newer versions of Geany have been released, they may have overcome this problem, but I still install Geany after JDK (Java Development Kit) to avoid issues.

Tuesday, 3 May 2011

Sun Java as default in Ubuntu

If you're using Ubuntu  11.10 (oneiric ocelot), please check this entry. Since 11.10, Sun/Oracle's Java JDK has been removed from all (Ubuntu/Canonical) repositories.

Just to summarise the information I got from

http://www.webupd8.org/2011/03/install-java-in-ubuntu-1104-natty.html
and
http://ubuntuforums.org/showthread.php?t=471488

in order to get Sun's Java Development Kit (JDK) up and running under Ubuntu (I'm using 11.04).

You may use Synaptic (the package manager) to add the "Canonical Partners" repository, reload, then find and install sun-java6-jdk.




Otherwise, open a terminal session and type the following:
sudo add-apt-repository "deb http://archive.canonical.com/ maverick partner"
or

sudo add-apt-repository "deb http://archive.canonical.com/ natty partner"


...depending on whether you have Ubuntu maverick (10.11) or natty (11.10). Then add:

sudo apt-get update
sudo apt-get install sun-java6-jdk
In case you had OpenJRE, OpenJDK, GCJ or similar (free, OSS Java alternatives) already in your system you may want to ensure Sun's JRE & JDK are the default. Still using the terminal, type:
sudo update-alternatives --config java
and select your preferred alternative from the list. To select a Java compiler, type
sudo update-alternatives --config javac
Now on to the testing. You should get the version numbers of the currently installed and default runtime and compiler by typing (in the terminal):
java -version
javac -version
...and that should be it.

I'd recommend you to use Geany as a source editor/IDE.

Tuesday, 23 February 2010

Java's static arg(h)s

The classic Java question when we finally manage to compile and run our "Hello world". What's with the public static void main(String args[]) line?
So our example is
public class TestArgs {
 public static void main(String args[]) {
  for (int i = 0; i < args.length; i++) {
   System.out.println("args[" + i + "] is " + args[i]);
  }
 }
}
I'm assuming you have some background knowledge here, and common sense. main( ) has to be public, since it is the main point of entry to our program and has to be easily accessible. As it is the first method that is run, it also has to be static, because we need to invoke it without instantiating its class. By this I mean that we do not need to create an object before we invoke the main method. Finally, void means that our main method returns nothing.

String args[] lets you pass arguments to your main method when you run your program. The argument(s) will be put in an array of string elements. The sample program prints all the arguments you pass via command line.

So a sample run should work like this:

javac TestArgs.java
java TestArgs uno 2 tres 4 cinco
Hello!
args[0] is uno
args[1] is 2
args[2] is tres
args[3] is 4
args[4] is cinco

Sunday, 24 January 2010

Ubuntu Linux: Install Sun's Java JDK

If you're using Debian (this works with Ubuntu as well, and may work with other Debian derivatives) and want to install Sun's Java Development Kit, and ensure it is set properly.
First, let's start by opening a terminal. Remember to use su or sudo for admin tasks.
sudo apt-get install sun-java6-jdk -y
You'll be asked to accept the accept the license terms before installing the JDK.
After all is done, type the following to test your JDK:
java -version
javac -version
Many Linux distributions come with other versions of the JRE (runtime environment). If this is the case, use the following terminal commands to choose which want to use as default:
update-alternatives --config java
update-alternatives --config javac
The first line lets you choose a JRE and the second, a compiler (provided more than one alternative is installed).

Notes: You need the nonfree/multiverse repository(-ies) enabled. OR, if you're using Ubuntu 10.04 onwards, the "partner" repository (Sun's/Oracle's Java packages have been moved to partner). Via command line, you may easily do this by pasting the following line in your terminal:

sudo add-apt-repository “deb http://archive.canonical.com/ maverick partner”
sudo apt-get update
 If you're using Ubuntu 10.10, the word in bold should be maverick; for 10.4, use lucid.

Wednesday, 6 January 2010

Java programming fun

This is an interesting problem solved using Java, but without using any fancy extra methods.

public class TestIsSubString {

/*
* Write a isSubString method that searches for a specific string within
* another string; the method must return true if the former exists in the
* latter string. Otherwise, the method return false.
*/

public static boolean isSubString(String part, String whole) {
// The hard way, with nested loops instead of whole.indexOf(part)!= -1
// Note to self: local variables MUST be init'd
int hits = 0; // flag type of variable
boolean isFound = false;
for (int i = 0; i <= whole.length()-part.length(); i++) {
hits = 0;
//isFound = false;
for (int j = 0; j < part.length(); j++) {
if( part.charAt(j) == whole.charAt(i + j) ) {
hits ++;
}
}
System.out.println("Hits = " + hits + " | len = " + part.length());
if( hits == part.length() ) {
isFound = true;
}
}
return isFound;
}

public static void main(String[] args) {
String text = "The cat in the hat.";

System.out.println("isSubString(\"cat\", \"The cat in the hat.\") "
+ isSubString("cat", text));

System.out.println("isSubString(\"bat\", \"The cat in the hat.\") "
+ isSubString("bat", text));

System.out.println("isSubString(\"The\", \"The cat in the hat.\") "
+ isSubString("The", text));

System.out.println("isSubString(\"hat.\", \"The cat in the hat.\") "
+ isSubString("hat.", text));

}
}

Optimise Windows 10/11

How to optimise your Windows setup Just in case you need it. If you want a safe and conservative approach, just disable the background apps ...