Calculate the Area of a Tetrahedron

sudhir sharma
Updated on 06-Aug-2019 06:46:49

191 Views

A tetrahedron is a pyramid with triangular base i.e. it has a base that is a triangle and each side has a triangle. All the three triangles converge to a point. As in the figure, Area of Tetrahedron = (√3)a2ExampleThe code to find the area of tetrahedron uses the math library to find the square and square-root of a number using sqrt and pow methods. For calculating the area we take a floating point and the value of the expression "((sqrt(3)*a*a))" is given to it.#include #include int main() {    int a= 5;    float area, volume;   ... Read More

Build DFA That Starts and Ends with 'a' from Input 'a' and 'b'

sudhir sharma
Updated on 06-Aug-2019 06:41:47

1K+ Views

DFA stands for Deterministic Finite Automata. It is a finite state machine that accepts or a string based on its acceptor.Here, we are going to make a DFA that accepts a string that starts and ends with a. The input is from the set (a, b). Based on this we will design a DFA. Now, Let's discuss some valid and invalid cases that are accepted by a DFA.Strings that are accepted by DFA: ababba, aabba, aa, a.Strings that are not accepted by DFA: ab, b, aabab.ExampleThis program check for a string that starts and ends with a. This DFA will ... Read More

Choose Between String and StringBuffer in Java

Maruthi Krishna
Updated on 05-Aug-2019 14:42:20

679 Views

The String type is a class in Java, it is used to represent a set of characters. Strings in Java are immutable, you cannot change the value of a String once created.Since a String is immutable, if you try to reassign the value of a String. The reference of it will be pointed to the new String object leaving an unused String in the memory.Java provides StringBuffer class as a replacement of Strings in places where there is a necessity to make a lot of modifications to Strings of characters.You can modify/manipulate the contents of a StringBuffer over and over ... Read More

Access Static Variable of Outer Class from Static Inner Class in Java

Maruthi Krishna
Updated on 05-Aug-2019 14:39:31

1K+ Views

A class with in another class is known as inner class, you cannot declare a class static unless it is an inner class. A static inner class is just like other class variables. You can access it (static inner class) without instantiationExampleYou can access the static variable of an outer class just using the class name. Following Java example demonstrates how to access static variables of a class from a static inner class.public class Outer {    static int data = 200;    static class InnerDemo {       public void my_method() {          System.out.println("This is ... Read More

Why Interfaces Don’t Have Static Initialization Block in Java

Maruthi Krishna
Updated on 05-Aug-2019 14:28:28

2K+ Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.A static method is declared using the static keyword and it will be loaded into the memory along with the class. You can access static methods using class name without instantiation.Static methods in an interface since java8Since Java8 you can have static methods in an interface (with body). You need to call them using the name of the interface, just like static methods of a class.ExampleIn the following example, we are defining a static method in an interface and accessing ... Read More

Access Object of a Class Without Class Name from Static Context in Java

Maruthi Krishna
Updated on 05-Aug-2019 14:17:52

723 Views

The only possible solution is to get the stack trace of the current thread. Get the class name using an element in the stack trace. Pass it to the forName() method of the class named Class.This returns a Class object and you can get an instance of this class using the newInstance() method.Examplepublic class MyClass {    String name = "Krishna";    private int age = 25;    public MyClass() {       System.out.println("Object of the class MyClass");       System.out.println("name: "+this.name);       System.out.println("age: "+this.age);    }    public static void demoMethod() throws Exception {   ... Read More

Are Values Returned by Static Method Static in Java

Maruthi Krishna
Updated on 05-Aug-2019 12:44:44

3K+ Views

Whenever you return values from a static method they are either static nor instance by default, they are just values.The user invoking the method can use them as he wants. i.e. you can retrieve the values and declare them static.But, since you cannot declare variables of a method static if you need to declare the vales returned by a method static you need to invoke it in the class outside the methods.ExampleAssume we have a class with name Demo as −class Demo{    int data = 20;    public Demo(int data){       this.data = data;    }   ... Read More

Why This Keyword Cannot Be Used in the Main Method of Java Class

Maruthi Krishna
Updated on 05-Aug-2019 12:42:03

1K+ Views

The static methods belong to the class and they will be loaded into the memory along with the class. You can invoke them without creating an object. (using the class name as reference).Examplepublic class Sample{    static int num = 50;    public static void demo(){       System.out.println("Contents of the static method");    }    public static void main(String args[]){       Sample.demo();    } }OutputContents of the static methodThe "this" keyword is used as a reference to an instance. Since the static methods doesn’t have (belong to) any instance you cannot use the "this" reference within ... Read More

Use 'this' Keyword in a Static Method in Java

Maruthi Krishna
Updated on 05-Aug-2019 12:17:53

6K+ Views

The static methods belong to the class and they will be loaded into the memory along with the class. You can invoke them without creating an object. (using the class name as reference).Examplepublic class Sample{    static int num = 50;    public static void demo(){       System.out.println("Contents of the static method");    }    public static void main(String args[]){       Sample.demo();    } }OutputContents of the static methodThe "this" keyword is used as a reference to an instance. Since the static methods doesn’t have (belong to) any instance you cannot use the "this" reference within ... Read More

What is setInterval Method in JavaScript

vineeth.mariserla
Updated on 05-Aug-2019 11:53:58

398 Views

setInterval()This is one of the many timing events. The window object allows code to execute at every certain interval of time. This object has provided SetInterval() to repeat a function for every certain amount of time. It takes two parameters as arguments. One is the function and the other is the time that specifies the interval after which the function should be repeated.syntaxwindow.setInterval(function, milliseconds, param1, param2, ...));This method can also take other parameters and can add it to the function.Example-1In the following example, setInterval() method is defined and a time interval of 3000 millisecs or 3 seconds is declared. Therefore the function provided ... Read More

Advertisements