Posts

Showing posts with the label java

Time Conversion Program in Java

Image
Introduction Time is the uncertain proceeded with advance of presence and occasions that happen in clearly irreversible progression from the past through the present to what's to come. Time is a part amount of different estimations used to succession occasions, to look at the term of occasions or the interims amongst them, and to evaluate rates of progress of amounts in material reality or in the cognizant experience. Time is frequently alluded to as a fourth measurement, alongside three spatial measurements. There are two time model which we are using up until now. they are 24-hour and 12-hour clock. The 24-hour clock is the tradition of time keeping in which the day keeps running from midnight to midnight and is partitioned into 24 hours, showed by the hours go since midnight, from 0 to 23. This framework is the most ordinarily utilized time documentation on the planet today. On the other hand, 12-hour clock is a time tradition in which the 24 hours of the day are pa...

Conditional Statement Introduction

Image
Conditional is an essential part in programming. You can do many things with it. In the past, I read a book about basic programming. The author said that most of programs written from various language are only contain  repetition and conditional. So this time I’ll give a solution from 30 days challenge code from hackerrank . This is really a simple program about selection of odd and even number. Okay, the image above is the condition which we should implement in listing code. It’s quite easy right. First,  you input integer number (not decimal). Remember the constraint is number you input is from 1 to 100. .Then you start with selection whether the number is odd or not. In mathematic we all know that odd number is an integer number that can not be divided by 2. So if we operate it with modulo operation, odd number will result 1. Odd number should print “Weird”. Back again, if the number is not odd then do a selection again for even number. The range already men...

Simple Cashier Program Source Code in Java

Image
Now we are already in Day 2 in 30 day code challenge from hackerrank . This task will test you some uses of operators. As you know mathematic operations have to use several operator like “+” for sum, “-” for subtraction, “*” for multiplication, “/” for division, “%” for modulo operation, “( )” for do an operation which inside of it first, and so on. In here, you will make a program like a cashier. Given a meal cost, tip percentage, and  tax percentage will be resulted a total amount of money customer must be paid. Image below show you how the program should be. After understanding the algorithm of program, then here the solution : import java.util.*; import java.math.*; public class Arithmetic { public static void main(String[] args) { Scanner scan = new Scanner(System.in); double mealCost = scan.nextDouble(); // original meal price int tipPercent = scan.nextInt(); // tip percentage int taxPercent = scan.nextInt(); // tax pe...

A Month of Coding Challenges with Java: Day 1 - Data Type

Image
After we step  from hello word, then we will forward with Day 1. This challenge will remind you the use of data type in programming language. Since I will java then data type in here will follow java programming rule. But at least, most of programming language have the similarity in data type. The purpose of this task is to make operation from variable which have same data type.. First variable s,i and d are already declared and initialized. Your job is to declare new variable which have similar data type. Then use + operator for each corresponded data type variables. Here listing program for this task. import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { int i = 4; double d = 4.0; String s = "HackerRank "; Scanner scan = new Scanner(System.in); /* Declare second integer, double, and String variabl...

A Month of Coding Challenges with Java: Day 0 - Hello World

Image
Hello world ! as I said in previous post, for the future post I will try to make solutions from challenges website. As for beginning to advanced we will start from 30 Days of Code Challenges . If you already registered in HackerRank you will asked if you want enroll this challenge. Well the purpose is to give you basic foundation. If you already finished one challenge you must wait for 24 hour to unlock the next challenge. If you tired of waiting you can enroll another challenge. There are so many of them, so you can choose as you like. So lets begin. We will start from Day 0 : Hello World. Most of programming tutorial will start to run hello world program. The purpose is to make sure that component of the said programming language is already installed properly in your PC. Besides that it gives you a nice introduction to that language. Imagine a toddler starts to learn speak from difficult word, I wonder how the outcome :D. So in the picture above the “Try Again” button used ...

Prime Number Sequence Program Source Code in Java

Image
Hello world ! In the past we have posted prime number sequence program source code in C. This time we'd like to share the same program but in Java. Before that, we have to know what is prime number. In mathematics, prime numbers are native numbers greater than 1, whose divisor is 1 and the number itself. 2 and 3 are primes. 4 is not a prime number because 4 can be divided by 2. package Prime; import java.util.Scanner; public class Prime { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.print(" Prime number limit : "); int num = in.nextInt(), ce, i, j, tot = 0, tamp[] = new int[4444]; for(i=2; i<=num; i++){ ce = 0; for(j=2; j<=i; j++) if(i%j == 0) ce++; if(ce == 1){ tamp[tot] = i; tot++;} } for(i=0; i<tot; i++) System.out.print(" " + tamp[i]); } } The result of listing program above is like this : If you have any question regarding this post, please leave a comment below. Pl...

Fibonacci Sequence Source Code in Java

Image
Hello World ! This time we'd like to share fibonacci sequence source in java. But firstly, we would like to give you short explanation about the sequence.  In mathematics, Fibonacci numbers are sequences that are recursively defined as follows: This sequence starts from 0 and 1, then the next number is obtained by adding two consecutive numbers. In short the name of sequence must have a pattern to make a series of number isn't it ? well if you want to know the theory about fibonacci just read this page (it is much easier to understand than wikipedia :p). Then this is source code of program to display fibonacci sequence  : import java.util.Scanner; public class Fibonacci { public static void main(String args[]) { int a = 1, b = 0, c, n, i = 0; System.out.println("Count Fibonanci Numbers!\n " + "Input number of series = "); Scanner in = new Scanner(System.in); n = in.nextInt(); while(i < n){ c = a + b; a = b; b = c...

Determine Leap Year Program using Java

Image
As we know  leap year is a year that adds a day in order to adjust the calendar to the astronomical year. In a year does not exactly consist of 365 days, but 365 days 5 hours 48 minutes 45.1814 seconds. If this is ignored, then every 4 years will be short of almost 1 day (exactly 23 hours 15 minutes 0.7256 seconds). So to compensate for this, every 4 years (4 divided years), given an extra day: February 29th. But since 5 hours 48 minutes 45.1814 seconds is less than 6 hours, then the years that can be divided 100 (as in 1900), not leap years, except can be divided by 400 (as in 2000). you can find more information regarding leap year in here . So, we shared a program to determine whether user input is leap year or not. import java.util.Scanner; public class Kabisat{ public static void main(String args[]) { int year; Boolean cek = new Boolean(false); System.out.println(" Input year :"); Scanner in = new Scanner(System.in); year = in.nextInt(); if(ye...

Java Program Source Code for Converting a value from Celsius to Fahrenheit or Vice Versa

Image
Hello world ! This time we'd like to share you java source code for program to convert a value from Celsius to Fahrenheit or vice versa. import java.util.Scanner; public class Konversi { public static void main(String[] arguments) { Scanner in2; double res = 0; System.out.println("Please select,\n1. C to F\n2. F to C"); Scanner in1 = new Scanner(System.in); System.out.print("= "); switch(in1.nextInt()){ case 1: System.out.println("C = "); in2 = new Scanner(System.in); res = in2.nextInt() * 9 / 5 + 32; break; case 2: System.out.println("F = "); in2 = new Scanner(System.in); res = (in2.nextInt() - 32) * 5 / 9; break; default: System.err.println ("Unrecognized option"); break; } System.out.println("\n Result is " + res); }} Here's how the result look like : The program will ask you what conversion you world like to choose. If you sel...

Java Program : Calculate The Area and Circumference of Circle

Image
Hello world ! This time we'd like to share you how to calculate the area and circumference of a circle. import java.util.Scanner; import java.util.Scanner; import java.lang.Math.*; public class LuasKeliling { public static void main(String[] arguments) { Scanner in2; double res = 0; System.out.println("Please select,\n1. to define circumference of circle\n2. to define area of circle"); Scanner in1 = new Scanner(System.in); System.out.print("= "); switch(in1.nextInt()){ case 1: in2 = new Scanner(System.in); System.out.println(" Radius = "); res = Math.PI * in2.nextInt() * 2; break; case 2: in2 = new Scanner(System.in); System.out.println(" Radius = "); res = Math.PI * Math.pow(in2.nextInt(), 2); break; default: System.err.println ("Unrecognized option"); break; } System.out.println("\n Result is " + res); } } The result of listing program abo...

Algorithm and Flowchart Simple Calculator

Image
Algorithm :  Initialize val1, val2, oprs, result  Input val1  Input val2  Choose operation (oprs) from +, -, x or :  If you choose operation (+), then the result = val1 + val2  If you select operation (-), then result = val1 - val2  If you select operation (x), then result = val1 * val2  If you select operation (:), then result = val1 / val2  Print results And this is the flowchart :

Algorithms and Programming Languages : How to implement algorithm in some programming languages?

Image
Algorithms and Programming Languages : How to implement algorithm in some programming languages? Consider the following example algorithm used to decompress the contents of array X (i) as below: The above algorithm consists of instructions. Although the instructions are written in codes close to computer programming languages, but the computer can not understand and execute those instructions. Computers can only understand if the instruction is written in a language called programming language. Here is given example of some programming languages as implementation of the above algorithm: It appears that the instructions in the Algorithm are intentionally made close to the instructions used in the programming language. So if it has been written the algorithm to solve a problem, then we can easily convert algorithm into form of programming language.

What is Java ?

Image
What is Java ?  Java is a programming language as well as a platform. And java is a high level programming language, powerful (for java error), safe. Then what is platform? Platform is a hardware or software environment where the program runs. Java has its own Java Runtime Environment platform. Below is an example listing code in java. Just an example, an explanation of what is meant in the coding below will be explained in the next post. Then the question, where can we use java? Based on the explanation from Sun, there are 3 billion devices that can run java. So java is used in many devices, for example its use like this: Desktop application like acrobat reader, media player, anti virus , and so on Web application like javapoint.com  Enterprise application or can be said an application whish is used by big company Mobile like Android Embedded System Smart card  Robotic Game ,and so on There are 4 main types of applications that can be ...