Category Archives: Programming

Abstract Class in Java

Java Abstract classes are used to declare common characteristics of subclasses. An abstract class cannot be instantiated. It can only be used as a superclass for other classes that extend the abstract class. Abstract classes are declared with the abstract keyword. Abstract classes are used to provide a template or design for concrete subclasses down the inheritance tree.

Like any other class, an abstract class can contain fields that describe the characteristics and methods that describe the actions that a class can perform. An abstract class can include methods that contain no implementation. These are called abstract methods. The abstract method declaration must then end with a semicolon rather than a block. If a class has any abstract methods, whether declared or inherited, the entire class must be declared abstract. Abstract methods are used to provide a template for the classes that inherit the abstract methods.

Abstract classes cannot be instantiated; they must be subclassed, and actual implementations must be provided for the abstract methods. Any implementation specified can, of course, be overridden by additional subclasses. An object must have an implementation for all of its methods. You need to create a subclass that provides an implementation for the abstract method.

http://www.javabeginner.com/learn-java/java-abstract-class-and-interface

Java – How to read input from console

JDK 5.0 provides a feature to read input from console. Java.util,Scanner is used for this purpose. This code read a String and an Integer from the console and stores them in the variables.
import java.util.Scanner;

public class InputExp {

public static void main(String[] args) {

String name;
int age;
Scanner in = new Scanner(System.in);

// Reads a single line from the console  and stores into name variable
name = in.nextLine();

// Reads a integer from the console  and stores into age variable
age=in.nextInt();
in.close();

// Prints name and age to the console
System.out.println(“Name :”+name);
System.out.println(“Age :”+age);

}
}

http://www.java-tips.org/java-se-tips/java.util/how-to-read-input-from-console.html

CPP – Set decimal precision

// setprecision example
#include <iostream>
#include <iomanip>
using namespace std;

int main () {
double f =3.14159;
cout << setprecision (5) << f << endl;   //3.1416
cout << setprecision (9) << f << endl;   //3.14159
cout << fixed;
cout << setprecision (5) << f << endl;   //3.14159
cout << setprecision (9) << f << endl;   //3.141590000
return 0;
}

http://www.cplusplus.com/reference/iostream/manipulators/setprecision/