12
Accessors are methods defined inside classes to access the private variables of the class. It is always a good practice to have instance variables as private so that, other classes cannot access them directly. This would avoid unwanted modification of data. These variables can be accessed only via their respective accessor methods. Ex: public class Test { private String name = ""; public String getName(){ return this.name; } Public void setName(String val){ this.name = val; } } Here getName and setName are the accessor methods for the variable name.
Whati is Constructor overloading?
Constructor overloading is the feature by which we declare multiple constructors for a single class. Ex: let us say we want to create multiple constructor for a class Test
Public class Test {
Public Test() {
//code
}
Public Test(int vals) {
//code
}
Public Test(String val) {
//code
}
}
Write a method that takes 3 values as input parameters and returns the largest of three values?
public int max(int x, int y, int z){ return Math.max(x,Math.max(y,z)); }
The method Math.max() only accepts 2 arguments, so you need to perform this method twice if you want to compare 3 numbers, as per the code above.
What are the examples of string constant?
In Java, a string constant is a text within double quotation marks. Example:
String playerName = "Player 1"; // Assign a default name initially
Here, the string constant was assigned to variable playerName. If you don't want the value to change later, declare it as final:
final String greeting = "Hello!"
How can web tools such as java or ActiveX be used for malicious attack?
by installing unwanted programs on a computer
Where can one view oops pictures?
Comedy pictures such as oops pictures can be viewed online on many websites or in some funny magazines. Examples of websites that contain funny oops pictures are the websites izismile and ebaumsworld.
What happen if you omit public in java programming?
If you omit either the "public" or the "private" keywoard, the access (to a field or method) will be the so-called "default access". The field or method will be visible for other classes, if they are in the same package.
You have to correctly identify the type of data, for storage efficiency, but even more important, to have the required flexibility. For example, you may decide to store a telephone number as a number. Later you find out that this is an invitation to trouble: if you define it as a number, you can only store digits, but no hyphens, spaces, or parentheses. So, in this case, some kind of alphanumeric data type (text, string; names vary in different programming languages) is more appropriate.
You should give your variable names so that they can be recognized. In a quick exercise, you may work with 3 or 4 variables, so you won't notice the difference. But in a real-world program, you may have scores, or hundreds, of variables; program maintenance would be a nightmare if you can't figure out what several of the variables are used for.
You have to correctly identify the type of data, for storage efficiency, but even more important, to have the required flexibility. For example, you may decide to store a telephone number as a number. Later you find out that this is an invitation to trouble: if you define it as a number, you can only store digits, but no hyphens, spaces, or parentheses. So, in this case, some kind of alphanumeric data type (text, string; names vary in different programming languages) is more appropriate.
You should give your variable names so that they can be recognized. In a quick exercise, you may work with 3 or 4 variables, so you won't notice the difference. But in a real-world program, you may have scores, or hundreds, of variables; program maintenance would be a nightmare if you can't figure out what several of the variables are used for.
You have to correctly identify the type of data, for storage efficiency, but even more important, to have the required flexibility. For example, you may decide to store a telephone number as a number. Later you find out that this is an invitation to trouble: if you define it as a number, you can only store digits, but no hyphens, spaces, or parentheses. So, in this case, some kind of alphanumeric data type (text, string; names vary in different programming languages) is more appropriate.
You should give your variable names so that they can be recognized. In a quick exercise, you may work with 3 or 4 variables, so you won't notice the difference. But in a real-world program, you may have scores, or hundreds, of variables; program maintenance would be a nightmare if you can't figure out what several of the variables are used for.
You have to correctly identify the type of data, for storage efficiency, but even more important, to have the required flexibility. For example, you may decide to store a telephone number as a number. Later you find out that this is an invitation to trouble: if you define it as a number, you can only store digits, but no hyphens, spaces, or parentheses. So, in this case, some kind of alphanumeric data type (text, string; names vary in different programming languages) is more appropriate.
You should give your variable names so that they can be recognized. In a quick exercise, you may work with 3 or 4 variables, so you won't notice the difference. But in a real-world program, you may have scores, or hundreds, of variables; program maintenance would be a nightmare if you can't figure out what several of the variables are used for.
Write a program to calculate sum of 50 num in java?
import java.util.Scanner;
public class Summer{
public static void main (String[]args){
int sum=0;
for(int i=1; i<=50; i++){
System.out.println("Enter the number (50 times)");
Scanner kb = new Scanner (System.in);
int input=kb.nextInt();
sum+=input;
}
System.out.println("The sum" +sum);
}
}
Can you have an interface without any abstract method?
My answer to "Can an interface without any abstract method?" is YES.
If abstract methods do not include the properties, then this interface should have at least 1 property (get or set) defined. It makes sense to have at least one of the abstract method or property/mutator/getter-setter.
If the interface contains no properties, no methods, I cannot think of a reason to create such an interface, meaning - it is useless.
How are exceptions handled in Java?
Exception handling if done by
1)Error detection
2)Error Reporting
3)Error Handling
Is implemented by following keywordstry,catch,finally,throws,throws
try{}
If exception occurs in try block the appropriate exception handlers that is associated with the try block handles the exception.
try block must have atleast one catch block follows it immediately
catch(Exception e){}
The catch statement take object of an exception as parameter .
If an exception is thrown the statements in the catch block is executed.
There should not be any statement between try and catch block.
we can have more then one catch block for one try block.
finally{}
It not mandatory to have a finally block
Sometime it is necessary to process certain statement whether exception occur or not
finally block exceutes even if exception occurs or not
Other then try catch block we directly handle a exception by using throws keywordthrow keyword allows us to create user define exception
Can you have nested switch case statement in C?
If that's what you really want to do, the compiler will allow it. It might indicate an inefficient design, though.
How do you print message before main in java?
You cannot do that. The main method of a java class is the point where the execution begins. You can print messages only after a main method is invoked.
How can you create java average calculator..any integer number.using netbeans or notepad plus plus?
import java.util.*;
public class avg {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many numbers: ");
int amount = input.nextInt();
double [] numbers = new double[amount];
double count = 0;
double avg;
for(int i = 0; i < numbers.length; i++){
System.out.print("Number: ");
numbers[i] = input.nextDouble();
count += numbers[i];
}
avg = count / amount;
System.out.println(avg);
}
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int stack[10],n=0;
int fact(int);
int pop();
void push(int);
main()
{
int num;
printf("\n FACTORIAL USING STACK IMPLEMENTATED WITH ARRAY\n");
printf("\n Enter a number whose factorial is to be found(1-9): ");
scanf("%d",&num);
printf("\n The factorial is : %d",fact(num));
getch();
} void display()
{
int i;
printf("\n The stack is :\n");
for(i=1;i<=n;i++)
{
printf("%d\t",stack[i]);
}
}
int pop()
{ if(n==0)
{
printf("\n Stack empty!Pop not possible!");
exit(1);
}
else
{
int res=stack[n];
--n;
display();
return res;
}
} void push(int x)
{
if(n==9)
{
printf("\n Stack full!Push not possible");
exit(1);
}
else
{
n++;
stack[n]=x;
display();
}
}
int fact(int num)
{
if(num==2)
{
push(2);
push(1);
int res=1;
display();
while(n>=1)
res*=pop();
return res;
} else
{
push(num);
return fact(num-1);
}
}
"Oak" was just a working project name used, supposedly named after an oak tree outside James Gosling's office window. When it was officially released, they didn't want to keep the current project name and chose to switch it to "Java".
What is Definition of java package with example?
A package is a mechanism for grouping related classes into a "group", called package. The package keyword identifies a file as belonging to a certain package.
Example: Put the following at the beginning of your Java source code:
package samplePackage;
A package is a mechanism for grouping related classes into a "group", called package. The package keyword identifies a file as belonging to a certain package.
Example: Put the following at the beginning of your Java source code:
package samplePackage;
A package is a mechanism for grouping related classes into a "group", called package. The package keyword identifies a file as belonging to a certain package.
Example: Put the following at the beginning of your Java source code:
package samplePackage;
A package is a mechanism for grouping related classes into a "group", called package. The package keyword identifies a file as belonging to a certain package.
Example: Put the following at the beginning of your Java source code:
package samplePackage;
What is the difference between garbage collector and finalization?
When all references to an object is Java are gone, the garbage collector will come along and free up the memory used by that object.
Every Java object has a method named finalize, which is called by the garbage collector when the memory for that object is being deallocated. "Finalization" is just a name given to the act of calling the finalize method during garbage collection.
False. A virtual base class is one that is common to two or more derived classes that are themselves base classes, and that may be combined through multiple inheritance. By declaring the common base class to be virtual in its direct derivatives, only one instance of the common base class exists in the multiple-inheritance classes.
What are some functions of Jboss Application Server?
The functions of the Jboss Application Server is to act as an open source code tool to allow individuals or businesses to create applications. It can be used for debugging and as a learning tool. It is based on Java.
What is the difference between Java 1.6 and Java 1.7?
Java 1.7 supports the features of 1.6 but introduces some new conventions:
- Java 7 allows use of String object in the expression of a switch statement.
- Auto-closing of resources (also known as try-with-resources)
- Allow catching of multiple Exceptions
- Allows underscores in Numeric Literal for readability (e.g. 1_234_567_890_123_456L)
- Substitute the parameterized type of the constructor with an empty set of type parameters (<>)
For full list see Java Programming Language Enhancements.
How to program a man walking using java code?
Source code:
001#include<graphics.h>
002using namespace std;
003
004int main()
005{
006 int gd=DETECT, gm;
007 initgraph(&gd, &gm,NULL);
008 int i,walk=0;
009
010 setcolor(BROWN);
011 bar3d(10,280,getmaxx()-50,320,10,50);
012 outtextxy(200,300,"www.2k8618.blogspot.com");
013
014 setcolor(WHITE);
015 circle(70,140,30);
016 circle(60,130,3);
017 circle(80,130,3);
018 line(70,135,70,145);
019 //line(65,155,75,155);
020 arc(70,140,60,120,15);
021
022 line(70,170,70,220);
023 line(70,170,50,220);
024 line(70,170,90,220);
025 line(70,220,55,270);
026 line(70,220,85,270);
027
028 setcolor(9);
029 fillellipse(150,100,60,40);
030 outtextxy(110,90,"hi," );
031 outtextxy(110,100,"go for a walk?");
032
033 delay(5000);
034
035 for(i=0;i<30;i++)
036 {
037
038 /*
039 cleardevice();
040 walk+=5;
041 bar3d(10,280,getmaxx()-50,320,10,50);
042 circle(70+walk,140,30);
043 line(70+walk,170,70+walk,220);
044 line(70+walk,170,40+walk,220);
045 line(70+walk,170,100+walk,220);
046 line(70+walk,220,30+walk,270);
047 line(70+walk,220,110+walk,270);
048 delay(500);*/
049
050 walk+=5;
051
052 cleardevice();
053
054 setcolor(BROWN);
055 bar3d(10,280,getmaxx()-50,320,10,50);
056 outtextxy(200,300,"www.2k8618.blogspot.com");
057
058 setcolor(WHITE);
059 circle(70+walk,140,30);
060 line(70+walk,170,70+walk,220);
061 line(70+walk,170,50+walk,220);
062 line(70+walk,170,90+walk,220);
063 line(70+walk,220,55+walk,270);
064 line(70+walk,220,85+walk,270);
065
066 delay(250);
067
068 walk+=5;
069
070 cleardevice();
071
072 circle(70+walk,140,30);
073 line(70+walk,170,70+walk,270);
074
075 setcolor(BROWN);
076 bar3d(10,280,getmaxx()-50,320,10,50);
077 outtextxy(200,300,"www.2k8618.blogspot.com");
078
079 delay(250);
080
081 walk+=5;
082
083 cleardevice();
084
085 bar3d(10,280,getmaxx()-50,320,10,50);
086 outtextxy(200,300,"www.2k8618.blogspot.com");
087
088 setcolor(WHITE);
089 circle(70+walk,140,30);
090 line(70+walk,170,70+walk,220);
091 line(70+walk,170,60+walk,220);
092 line(70+walk,170,80+walk,220);
093 line(70+walk,220,55+walk,270);
094 line(70+walk,220,85+walk,270);
095
096 delay(250);
097
098 }
099
100 circle(60+walk,130,3);
101 circle(80+walk,130,3);
102 line(70+walk,135,70+walk,145);
103 line(65+walk,155,75+walk,155);
104 //arc(70,140,60,120,15);
105
106 setcolor(8);
107 fillellipse(walk-10,100,60,40);
108 outtextxy(walk-30,100,"lonely...");
109
110 delay(2500);
111
112 getch();
113 closegraph();
114 return 0;
115}
Answer
A Java Drive-By is a Java Applet that is coded in Java and is put on a website. Once you click "Run" on the pop-up, it will download a program off the internet. This program can be a virus or even a simple downloader. If you'd like to get the source code or wanna know more information about a Java Drive-By, use Google.
What is the use of 'Dialog' class in java?
From the Java API: "A Dialog is a top-level window with a title and a border that is typically used to take some form of input from the user."
Dialogs are useful specifically to take input because they can be made to block input to other Window objects until the user finishes with the Dialog (think: Save File dialog box).