Learning Java

Explore the world of programming using Java!

Introduction to Java

1.1 -- Install Java and Eclipse IDE
1.2 -- Setup Java Project
1.3 -- Compiling the Program
1.4 -- Running and Debugging the Program

Control Flow Statements

1.1 -- if-then and if-then else statements
1.2 -- For Statements
1.3 -- Switch and While Statements
1.4 -- Branching Statements

Object Oriented Concepts

1.1 -- Classes and Objects
1.2 -- Encapsulation
1.3 -- Inheritance
1.4 -- Polymorphism

Data Structure Concepts

1.1 -- Stack
1.2 -- Queue
1.3 -- Sets and Maps
1.4 -- LinkedLists

Java Online IDE

public class Codersstack{ public static void main(String str[]){ System.out.println("Happy Coding"); } }

Thursday, December 29, 2022

Stack Example

/**
 * Create a BookStack program and add the books and print the books.
 * Stack - Last In First Out.
 */
import java.util.Stack;

public class BookStack {

	public static void main(String str[]) {

		Stack<String> bookStack = new Stack<String>() ;

		bookStack.push("Book1") ;
		bookStack.push("Book2") ;
		bookStack.push("Book3") ;

		System.out.println(bookStack.pop());
		System.out.println(bookStack.pop());
		System.out.println(bookStack.pop());
	}
}

Hello World Java Program


/* 
 * Welcome to CodersStack - HelloJavaBeginners.java
 */
public class HelloJavaBeginners
{
    public static void main(String[] args) {
        System.out.println("Hello Java Beginners! Welcome to CodersStack");
    }
} 

Convert Fahrenheit To Celcius Example

import java.util.Scanner;
/**
 * 
 * Celsius = (Fahrenheit -32)* (5.0/9)
 *
 */
public class FahrenheitToCelcius
{
	public static void main(String[] args) {
		Scanner celsiusScanner = null ;
		try {
			celsiusScanner = new Scanner(System.in);
			System.out.print("Enter a degree in Fahrenheit: ");
			double inFahrenheit = celsiusScanner.nextDouble();
			double celsius = (inFahrenheit - 32) * (5.0 / 9) ;
			System.out.println(String.format("%s Fahrenheit = %s Celsius", inFahrenheit , celsius));
		}finally {
			celsiusScanner.close() ;
		}
	} 
}    

Generate A Web Page Example

import java.io.BufferedWriter;
import java.io.FileWriter;

/**
 * 
 * HtmlFileWriter
 *
 */

public class HtmlFileWriter
{    
	static String htmlTemplate = "<!DOCTYPE html><html>" +
			"<body style=\"background-color:white;\"><h1 style=\"color:blue;text-align:center;\">" +
			"Welcome to <REPLACE_HEADER> Web Page !!!</h1>" +
			"<P style=\"color:green;\"><REPLACE_PARAGRAPH></p>" +
			"<center><button style=\"color:red;\" type=\"button\"onclick=\"document.getElementById('myid').innerHTML = Date()\">Display Date and Time.</button><p id=\"myid\"></p></center>" +
			"</body></html>" ;

	public static void main(String str[]) throws Exception {
		writeToHtmlFile("Student Name", "Output") ;     
	}

	public static void writeToHtmlFile(String name, String output) throws Exception {

		try (BufferedWriter br = new BufferedWriter(new FileWriter(String.format("C:\\javaoutput\\%s.html", name)))) {
			htmlTemplate = htmlTemplate.replaceAll("<REPLACE_HEADER>", name) ;
			htmlTemplate = htmlTemplate.replaceAll("<REPLACE_PARAGRAPH>", output) ;            
			br.write(htmlTemplate);
		}
		System.out.println("Html output file generated"); 
	}
}

Weekly TimeTable Example

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * This is a simple example to display predefined activities of a week.

 * What you learn in this program?
 * 
 *   - The conditional statement : switch and break
 *   - Simple methods and method signature
 *   - Calling methods from other methods
 *   - Java Date class to get today's date.
 *   - Building your own simple weekly TimeTable
 *
 *
 *  
 * -- Add your own activities for the week in the printTodaysActivities method.
 * -- Run the program each day to see the activities for the day.
 *
 *
 */
public class WeeklyTimeTable
{        
	public static void main(String str[]){        

		printTodaysActivities(dayOfTheMonth()) ;

	}

	public static void printTodaysActivities(String today){

		switch(today){

		case "Monday" :
			System.out.println("Monday activity") ;
			break ;

		case "Tuesday" :
			System.out.println("Tuesday activity") ;
			break ;

		case "Wednesday" :
			System.out.println("Wednesday activity") ;
			break ;

		case "Thursday" :
			System.out.println("Thursday activity") ;
			break ;

		case "Friday" :
			System.out.println("Friday activity") ;
			break ;

		case "Saturday" :
			System.out.println("Saturday activity") ;
			break ;

		case "Sunday" :
			System.out.println("Sunday activity") ;
			break ;   
		}
	}

	public static String dayOfTheMonth() {        
		Date today = new Date() ;
		SimpleDateFormat sdf = new SimpleDateFormat("EEEEE") ;
		String day = sdf.format(today) ;
		return day ;
	}
}

Student Class Example

/**
 *  A class is a blueprint from which individual objects are created.
 *  Sample Student Class java program
 * How to create objects?
 *  Student student = new Student("My name","Grade1");
 **/

public class Student {

	String grade ;
	String studentName ;

	public Student(String studentName, String grade){            
		this.studentName = studentName ;
		this.grade = grade ;
	}
	public String getGrade()
	{
		return grade;
	}
	public void setGrade(String gradeName)
	{
		this.grade = gradeName;
	}
	public String getStudentName()
	{
		return studentName;
	}
	public void setStudentName(String studentName)
	{
		this.studentName = studentName;
	}
	
	public static void main(String str[]) {
		
		Student student = new Student("Student1", "7th") ;
		
		System.out.println("Student Name " + student.getStudentName());
		System.out.print("Student Grade " + student.getGrade());
		
	}
}

Data Structures - Queue Example

import java.util.LinkedList;
import java.util.Queue ;

/**
 * 
 * Queue is First In First Out Data structure.
 *
 */
public class TicketQueue
{   
    public static void main(String str[]){
        Queue<String> ticketQueue = new LinkedList<String>() ;        
        ticketQueue.add("A") ;
        ticketQueue.add("B") ;
        ticketQueue.add("C") ;
        
        System.out.println(ticketQueue.poll());
        System.out.println(ticketQueue.poll());
        System.out.println(ticketQueue.poll());
        
    }
}

Followers