Posts

Showing posts from April, 2019

Convert Celsius to Fahrenheit

Image
Convert temperature in degree Celsius to Fahrenheit Different methods are defined to convert temperature into Fahrenheit. you can define the sub-methods and the main method in the same class or the two different two classes, and here we did the same. The calculation method ' Calculate ' and the setter method ' setc ' is defined in a class called ' Conversion ' and the main method is defined in another class named ' Test '. Getter methods are used to set the value in variables. The data gets set here when the values are passed as an argument form another method that might be the simple method or the main method. The syntax for the setter method is, public void [methodName](Parameter) { Statement(s); } Here is the program is written inside the class ' Calculate '. And the main function written inside the Test class is. public  class  Test {      public static void main(String []args) ...

Calculate the area of Triangle

Image
Calculate the Area of Right angled Triangle Parameterized Methods are defined to calculate the area of a simple triangle and the Right angled triangle and the values are passed as an argument from the main function. Values are taken from the user with the help of  Scanner  (built-in class of  util  package in  java  project ). import   java . util . Scanner ; //Calling Scanner Class public class  TriangleArea {        //Declaring Variables        private   int  height;        private  int  base;        private  int  a;        private  int  b;        private  int  c;        private  int  s;        private  boolean  T;        public void  Area( int  height,  int...

Java program to calculate the simple interest Using 'Scanner' Class

Calculate the Simple Interest The data needed to calculate simple interest can be initialized with the default value inside the class and also can be taken from the user. Here ' Scanner ' class, the built-in class inside the ' util ' package of ' java ' project is imported to get value from the keyboard. import   java . util . Scanner ;  //Calling Scanner class to read input from Keyboard public class   SimpleInterest {      public static void main(String []args)     {           //Declaring Variables           double  Principal;           double  Time;           double  Rate;           double  Interest;           System.out.println ("Enter amount of Principle");  //Instruction for User          Principal=  new  Scanner (...

Calculate the area of Rectangle Using Constructor Overloading rules

Example of Constructor Overloading in Java program This is a java program to calculate the area of Triangle. This program is helpful to understand the constructor overloading. Here many constructors are called with the same name as same as of the class name 'Rectangle'. public class Rectangle { //Declaring global variables int length; int breadth; double area; Declaring global variables is accessible from wherever you want. Here many constructors are called and they need to be stored in the same variable so the global variables are created. The variables ' length ', ' breadth ', and ' area'  stores the length breadth and the area of the rectangle respectively.       public  Rectangle()  //Non-Parameterised Constructor     {           this. length=4;           this. breadth=2;     } The constructors with no any parameters are called...

Basic Concept of Method In java

Image
Introduction to the method A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name.  It is also called as sub-program/function it often returns value. The method has its own name.  When that name is called in a program, the execution of the program goes inside the body of that method.  When the method is finished executing, it returns to the area of the program from which it was called, and the program continues on to the next line of code. Process of calling method is given below: - Good programmers write in a modular fashion which allows for several programmers to work independently on separate concepts which can be assembled at a later date to create the entire project.  The use of methods will be our first step in the direction of modular programming. Methods are time savers; in that, they allow for the repetition of sections of code without retyping t...

Concept of Constructor Overloading

Constructor Overloading Like methods, constructors can be overloaded. In other words, you can provide more than one constructor for a class if each constructor has a unique signature. Constructors with the same name as that of the Class name and has a different (number or Datatype) of parameters differ from each other is simply known as  constructor overloading.  If you have a class name 'Student' then the Eg of Constructor overloading in Student class be like:  public Student( String first, String last, double mark) {     firstName = first;     lastName = last;     Mark = mark; } public Student( String first, String last,) {     firstName = first;     lastName = last; } public Student( int  rollNo, String last,) {      R ollNo = rollNo;       lastName = last; } public Student( double mark ) { ...

Constructor in Java Programming

Concept of Constructor A  constructor   in Java is a block of code similar to a method it is called when an instance of an object is created. The key differences between a constructor and a method are given below: A constructor doesn’t have a return type. The name of the constructor must as same as the name of the class. It is considered as the other member function of a class. When an instance of the object is created it is called automatically. It is called at the time of Object creation. Syntax of the constructor: //This is the constructor public ClassName (Parameter list){ Statement(s)... } The public keyword in the constructor indicates that other classes can access the constructor. It can be defined under private and protected section but in such cases, the constructor is not available to the Non-member function. ClassName must be the same as the name of the class that contains the constructor. The constructo...

Use of Arithmetic Operators

Image
Use of Arithmetic operators in java program There are some Arithmetic Operators which helps to make a comparison between numbers. pic- operators' sign By the help of this picture, You can identify the proper operator and use it. The program below may help you to learn how the arithmetic operators can be managed properly. Output:-            Is 20>25? false            Is 20<25? true            Is 20>=25? false            Is 20<=25? true            Is 20 not equal to 25? true If you want the code in the editable format then, Please comment down below

Java Program to make a comparison between numbers

Conversion between Numbers public class Double Comparison {         public static void main (String []args)        {                   boolean  result;  //declaring  variable               int a=30 , b=27 ;  //value assign                result = (a>=b)&&(b>=a);  //comparison                System.out.println ("is (30>=27)and(27>=30) ? "+result);  //output                result = (a>=b)||(b>=a); //comparison                System.out.println ("Is (30>=27)or(27>=30) ? "+result);  //output          } }  Output of the given program is :- Is (30>=27)and(27>=30) ? false  Is (30...

Java program to print 'Hello World'

Simple program to print 'Hello World' This is the starting phase of the java program to print "Hello World". You can change the sentence inside the double quote sign like:- "Hello World" to "Hii This is my first java program", or anything according to your wish. public class  PrintHello {                public static void main(String []args)       {                              System.out.println (" Hello World  "); //Printing Hello                     } } Output of the above program is:  Hello World

Java program using Scanner Class

Scanner Class in Java The scanner is a class in java.util package used for obtaining the input of the primitive types like int, double, etc. and strings. It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming. To create an object of Scanner class, we usually pass the predefined object System.in, which represents the standard input stream. We may pass an object of class File if we want to read input from a file. To read the numerical values of a certain data type XYZ, the function to use is nextXYZ(). For example, to read a value of type short, we can use nextShort() To read strings, we use nextLine(). To read a single character, we use next().charAt(0). next() function returns the next token/word in the input as a string and charAt(0) funtion returns the first character in that string Program to calculate import java.util.Scanner; //Calli...

Java program using Increment and Decrement Operators

Image
Increment ++ and Decrement -- Operator as Prefix and Postfix in java Increment and decrement operators are unary operators that add or subtract one, to or from their operand ( an operand is the part of a computer instruction that specifies data that is to be operating on or manipulated and, by extension, the data itself.) respectively. In different programming language(Java, C, C++, Vala, PHP, etc…) the increment operator (++)   increases, the decrement operator (--) decreases the value of the variable(operand) by 1. let us suppose, num= 10      using increment operator, num++;           //Increase value by 1 and the value of num becomes 11 ++num;           //Increase value by 1 and the value of num becomes 11      using decrement operator, num--;             //Decrease value by 1 and the ...