Showing posts with label JAVA english version. Show all posts
Showing posts with label JAVA english version. Show all posts

Sunday, January 6, 2019

Examples of Simple Programs 1 & 2 Dimensional Array in Java

Examples of Simple Programs 1 & 2 Dimensional Array in Java

Previously you must have understood what it is to understand the array of variables easier.

So, like this simple example:

When you want to store a data that an instance name, of course, you need to declare a variable to be able to store data such name.

Unfortunately you do not always deal with one data, now you imagine if you want to list the names of students in your class.

Please note: a regular variable can only store one value.

Then, if you will create a variable name1, name2, name3, and so on?

Of course not, this is why you need an array data type. Because of the array allows you to store a lot of data in a variable.

Below is a simple example of the use of java for an array of one-dimensional and two-dimensional array number specified by input.

Example 1 dimensional array java program:

import java.util. *;
class input_Array1 {
public static void main (String [] xxx) {
try {
int arr [] = new int [4];
String array [] = new String [4];
Scanner s = new Scanner (System.in);
for (int i = 0; i <arr.length; i ++) {
System.out.print ("Enter the array value : ");
array [i] = s.next ();
arr [i] = Integer.parseInt (array [i]); // conversion of data types
}
for (int i = 0; i <arr.length; i ++)
System.out.print (arr [i] + " ");
}
catch (Exception e) {
System.err.println (e);
}
}
}

Example 1-dimensional array java program:
the results of coding, variable array will accommodate more than one value




Examples of java program 2 dimensional rows and columns:

import java.util. *;
public class ARRAYINPUT_2D {
public static void main (String [] args) {
Scanner scan = new Scanner (System.in);
String [] [] array = new String [4] [4];
for (int i = 0; i <array.length; i ++) {
for (int j = 0; j <array.length; j ++) {
System.out.print ("Input Number line [" + i + "]" + "column [" + j + "] =");
array [i] [j] = scan.next ();
}
System.out.println ("\n");
}
System.out.println ("\n");
System.out.println ("Result of Input Value");
for (int i = 0; i <array.length; i ++) {
for (int j = 0; j <array.length; j ++) {
System.out.print ("|" + array [i] [j] + "|");
}
System.out.println ("");
}
}
}

Examples of java program 2 dimensional rows and columns:
coding results, will be input and display the values ​​of variables array using two-dimensional array in the form of rows and columns



Reviewer: FajarYusuf.Com
ItemReviewed: Examples of Simple Programs 1 & 2 Dimensional Array in Java

Examples Problem Java Programming and Code

Example Example Problem Java Programming and codingnya

1.) Program determine type Triangle with Java 

Flat Triangle is bounded by three sides and has three vertices. Triangle has some kind of like an isosceles triangle, equilateral triangle, the triangle is arbitrary, etc. By using java, we can create a program to determine the type of triangle side lengths just by entering the triangle side.

Examples of the coding like this:
import java.util.Scanner; public class DeterminingTriangle { public static void main (String [] args) { Scanner scanner = new Scanner (System.in); int a, b, c; System.out.print ("Enter number 1:"); a = scanner.nextInt (); System.out.print ("Enter number 2:"); b = scanner.nextInt (); System.out.print ("Enter number 3:"); c = scanner.nextInt (); if(a==b&&b!=c&&a!=c||b==c&&b!=a&&a!=c||a==c&&b!=a&&b!=c) { // if only two numbers have the same value then print an isosceles triangle System.out.print ("input results make: isosceles triangle"); } else if (a == b || a == c || b == c) { // if all three numbers are the same then print an equilateral triangle System.out.print ("input results make: equilateral triangle"); } else // if the three sides are not the same then print an arbitrary triangle System.out.print ("the input results make: an arbitrary triangle"); } }

coding result, in the example I put 2 fatherly produce the same number of isosceles triangles
coding result, in the example I put 2 fatherly produce the same number of isosceles triangles



2.) Determine the Largest Numbers Java program, Smallest, and the average 

Next is a program that determines the largest number, smallest, and the average is determined by inputting three numbers. and by using the concept of branching.


Examples of the coding like this:

import java.util.Scanner; public class LargestSmallData { public static void main (String [] args) { System.out.println ("Specifies the Biggest, Smallest, and Average Numbers \n"); Scanner input = new Scanner (System.in); double x; double y; double z; double avg; System.out.print ("Enter First Number (X):"); x = input.nextDouble (); System.out.print ("Enter Second Number (Y):"); y = input.nextDouble (); System.out.print ("Enter Third Number (Z):"); z = input.nextDouble (); avg = (x + y + z) / 3; System.out.println ("\n Results:"); System.out.println ("The average is:" + avg); if (x == y && y == z) {System.out.println ("The Three Same Numbers are Large"); } else if (x> y && x> z) {System.out.println (x + "is the largest number"); } else if (y> z && y> x) {System.out.println (y + "is the largest number"); } else if (z> x && z> y) {System.out.println (z + "is the largest number"); } else if (x == y && x> z) {System.out.println (x + "is the largest number"); } else if (x == z && x> y) {System.out.println (x + "is the largest number"); } else {System.out.println (y + "is the largest number"); } if (x == y && y == z) {System.out.println ("The Three Small Same Numbers"); } else if (x <y && x <z) {System.out.println (x + "is the smallest number"); } else if (y <z && y <x) {System.out.println (y + "is the smallest number"); } else if (z <x && z <y) {System.out.println (z + "is the smallest number"); } else if (x == y && x <z) {System.out.println (x + "is the smallest number"); } else if (x == z && x <y) {System.out.println (x + "is the smallest number"); } else {System.out.println (y + "is the smallest number"); } } }

Examples Problem Java Programming and Code
coding result, in the example above shows the input of the largest, smallest and the average of the three input is entered




3.) For Counting Acquisition Bonus Program

Based java program serves to calculate the total price and bonus purchased
If the amount of price in the purchasing = 1500000 will earn bonus Iron
If the amount of price in the purchasing = 1000000 will get a bonus Umbrella
If the amount of price in the purchasing = 500000 then will get bonuses Ballpoint 


Examples of the coding like this
import java.util.Scanner; public class Bonuses { public static void main (String [] args) { // TODO code application logic here Scanner enter = new Scanner (System.in); String Bonus = null; System.out.print ("Enter Amount of Purchases ="); int A = enter.nextInt (); int D1 = A * 500000; System.out.print ("Buy Total Amount =" + A); System.out.print ("\n"); int result = ((D1)); System.out.print ("-------------------------- \n"); System.out.print ("Total price =" + result); System.out.print ("\n"); if (result>= 1500000) {Bonus = "CONGRATS YOU GET AN IRON BONUS";} else if (result>= 1000000) {Bonus = "CONGRATS YOU GET UMBRELLA BONUS";} else if (result>= 500000) {Bonus = "CONGRATS YOU GET BONUS BALLPOINT";} else {Bonus = "SEE YOU AGAIN TO COME BACK";} System.out.println ("" + Bonus); } }

coding results, in my example input 2 so that the total price to 1000000 and I get bonuses umbrella



Reviewer: FajarYusuf.Com
ItemReviewed: Examples Problem Java Programming and Code

Thursday, December 27, 2018

Object, Class, Message and Method JAVA

Object, Class, Message and Method JAVA

Object, Class, Message and Method

In object-oriented programming, a class is a template for structure definition and object creation. So the object of an instance of a class, that class is a collection of plans that specify how to build an object.

Object and Class

Information-related information and describes an object called object attributes (object attribute). This attribute allows an object has a value attribute that stands alone. Besides object attributes also have an action called by the method. So the method is a set of operations on an object in the form of code instructions.

General syntax format-making class is as follows:
class classname {
// declare instance variables var1 type;
var2 type;
// ... type varN;
// declare methods
type method1 (parameters) {
// body of method
}
type method2 (parameters) {
// body of method
}
// ...
type methodN (parameters) {
// body of method
}
}

Defining Class

Consider the example of a class definition of vehicles consisting of three instance variables and the making of objects of that class, such as vehicle class pembuaan below:
1public class Vehicle {
2int passenger; // number of people in the vehicle
3int capacity_bb; // fuel capacity
4int mpg; // consumption of miles per gallon of fuel
5}

Creating Objects

Objects created by the pre-defined class that is a class above the previous vehicle. 
so coding for the object as follows:
01public class VehicleObjects {
02public static void main (String [] args) {
03Vehicle minivan = new Vehicle ();
04Vehicle sportscar = new Vehicle ();
05int range1, range2;
06minivan.passenger = 7;
07minivan.capacity_bb = 16;
08minivan.mpg = 21;
09sportscar.passenger = 2;
10sportscar.capacity_bb = 14;
11sportscar.mpg = 12;
12range1 = minivan. capacity_bb * minivan.mpg;
13range2 = sportscar. capacity_bb * sportscar.mpg;
14System.out.println ("Minivan can carry "
15+ minivan. passenger + " person with range " + range1);
16System.out.println ("Sportscar can bring "
17+ sportscar. passenger + " person with range " + range2);
18}
19}

Creating Objects Java
coding results

Adding Method into Class

Class that has been made over there just an instance variable, now add some operation (method) into the class.

vehicle class with the method:
01public class Vehicle {
02int passenger; // number of people in the vehicle
03int capacity_bb; // fuel capacity
04int mpg; // consumption of miles per gallon of fuel
05String name = new String ();
06int getRange () {
07return mpg*capacity_bb;
08}
09double getNumBB (int miles) {// amount of fuel needs
10return (double) miles / mpg;
11}
12}

objekvehicle class with the method:
01public class VehicleObjects {
02public static void main (String [] args) {
03Vehicle minivan = new Vehicle ();
04Vehicle sportscar = new Vehicle ();
05double gallon;
06int distance = 259;
07minivan.name = "Minivan";
08minivan.passenger = 7;
09minivan.capacity_bb = 16;
10minivan.mpg = 21;
11sportscar.name = "Sportscar";
12sportscar.passenger = 2;
13sportscar.capacity_bb = 14;
14sportscar.mpg = 12;
15gallon = minivan.getNumBB (distance);
16System.out.println (minivan.name + " requires " + gallon +
17" gallons to travel distance " + distance + " miles.");
18gallon = sportscar.getNumBB (distance);
19System.out.println (sportscar.name + " requires " + gallon +
20" gallons to travel distance " + distance + " miles.");
21}
22}
Adding Method into Class Java
coding results

constructor

A constructor is an object initialization when it is created.  Constructor name equal to the name of its class. 

vehicle class with a constructor:
01public class Vehicle {
02int passenger; // number of people in the vehicle
03int capacity_bb; // fuel capacity
04int mpg; // consumption of miles per gallon of fuel
05String name = new String ();
06Vehicle (String name, int passenger, int capacity, int mpg) {
07this.name = name;
08this.passenger = passenger;
09this.capacity_bb = capacity;
10this.mpg = mpg;
11}
12int getRange () {
13return mpg * capacity_bb;
14}
15double getNumBB (int miles) {// amount of fuel needs
16return (double) miles / mpg;
17}
18}

objekvehicle class with a constructor:
01public class VehicleObjects {
02public static void main (String [] args) {
03Vehicle minivan = new Vehicle ("Minivan"7,16,21);
04Vehicle sportscar = new Vehicle ("Sportscar"2,14,12);
05double gallon;
06int distance = 259;
07gallon = minivan.getNumBB (distance);
08System.out.println (minivan.name + " requires " + gallon +
09" gallons to travel distance " + distance + " miles.");
10gallon = sportscar.getNumBB (distance);
11System.out.println (sportscar.name + " requires " + gallon +
12" gallons to travel distance " + distance + " miles.");
13}
14}
constructor JAVA
coding results

Reviewer: FajarYusuf.Com
ItemReviewed: Object, Class, Message and Method JAVA

TENTANG SITUS

SITUS INI ADALAH SITUS PEMBELAJARAN PEMROGRAMAN DAN JUGA PEMAHAMAN TERHADAP TEKNOLOGI KOMPUTER. KALIAN BISA MENGIKUTI PEMBELAJARAN PEMROGRAMAN DENGAN BERTAHAP PADA MATERI YANG SUDAH SAYA SEDIAKAN, JIKA ADA MATERI YANG MEMBINGUNGKAN SILAKAN LAKUKAN KOMENTAR PADA MATERI YANG ANDA TANYAKAN ATAU KALIAN BISA MENGHUBUNGI SAYA DIHALAMAN KONTAK.