answersLogoWhite

0

📱

Java Programming

The Java programming language was released in 1995 as a core component of the Java platform of Sun Microsystems. It is a general-purpose, class-based, object-oriented language that is widely used in application software and web applications.

5,203 Questions

Write a program to find the largest of n numbers in java?

Assuming the numbers are integers, and they are stored in an array called myArray. The following has not been tested, but gives the general idea: int max; max = myArray[0]; for (int i = 1; i < myArray.length(); i++) } if (myArray[i] > max) max = myArray[i]; System.out.println(max); }

Assuming the numbers are integers, and they are stored in an array called myArray. The following has not been tested, but gives the general idea: int max; max = myArray[0]; for (int i = 1; i < myArray.length(); i++) } if (myArray[i] > max) max = myArray[i]; System.out.println(max); }

Assuming the numbers are integers, and they are stored in an array called myArray. The following has not been tested, but gives the general idea: int max; max = myArray[0]; for (int i = 1; i < myArray.length(); i++) } if (myArray[i] > max) max = myArray[i]; System.out.println(max); }

Assuming the numbers are integers, and they are stored in an array called myArray. The following has not been tested, but gives the general idea: int max; max = myArray[0]; for (int i = 1; i < myArray.length(); i++) } if (myArray[i] > max) max = myArray[i]; System.out.println(max); }

How are exceptions generated?

Exceptions are generated by the Java Virtual Machine during program execution. When the JVM comes across a piece of code that it cannot execute properly or a piece of code that will create issues when the JVM executes it will generate an exception. Ex: divide by 0 or trying to access a null variable etc

The language also has syntax that can catch and handle these situations. It is called the try - catch - finally construct.

Can a single Java class subclass extend both InputStream and Reader. True or False?

Java does not have multiple inheritance, so no. Java can use multiple interfaces, though, with the "implements" keyword.

When was The Making of the English Working Class created?

The Making of the English Working Class was created in 1963.

What is field data type?

The data type of the Variable determines/indicates the type of data that can be stored in a field/variable

Can you name the legacy classes and interface for collections?

Dictionary, Hashtable ,Properties ,Stack and vector are the legacy classes in java

Which is better for creating cognos report front end either .NET or java?

Hello, the Cognos SDK is expected to be fully functional with either a .NET or Java interface. You may find an easier time implementing your solution with a Java solution only because the majority of the examples provided by Cognos are in Java. However, you can also use SOAP calls to invoke your Cognos application and won't have to worry about adding .DLLs. http://bicentresdkguide.blogspot.com

What is integrators in oops?

Intigrators is the highest/laatest chips processor in the last generation of computer that make the computer to speed.

Why do we use double in java but do not use float?

Double is more precise than float. The 4 bytes saved on a float are usually not very relevant. However, if you need to save large amounts of numbers (e.g. in an array), and you don't need the extra precision, you might save some memory by using float.

What are the methods and interfaces from string and vector class?

I bought a 1 ton split A\C this week. It's power consumption is 1000 W. The outer temperature in our area in day time is 34-36 degree Celsius. I set up the ac for a 27 degree which is comfortable for me. So i want to know, how to reduce then power consumtion of this ac. My room is much smaller 10(w) X 8(L) X 9(H) is the size. This ac will cool my room in just 1 minute. So after 10 minutes working, i will put this ac in standby mode in remote. After a 10 minutes, i will turn on this ac again. Does this method helps me to reduce the power consumption ?

What is meant by pedefined types?

Predefined types are the primitives that are included in Java, such as char and int. They are already defined by the language, and so do not need to be developed by a developer or included from any package. There are also predefined classes, which do need to be imported, but are again part of the language specification so the developer does not need to reinvent the wheel each time they start a new project.

What are all the different methods of accessing array elements?

#include<iostream>

#include<cassert>

int main()

{

char str[] = "The quick brown fox jumped over the lazy dog.";

// Access the 5th element ('q' @ index 4) in 6 different ways:

char a = *(str+4);

char b = *(4+str);

char c = str[4];

char d = 4[str];

char e = *(&str[2]+2);

char f = *(2+&str[2]);

// Point to 7th element and access the 5th element:

char* p = &str[6];

char g = *(p-2);

// Test for equality:

assert(a==b);

assert(b==c);

assert(c==d);

assert(d==e);

assert(e==f);

assert(f==g);

assert(g=='q');

}

Explanation:

There is really just one version, a, but just as x+y == y+x, it is commutative thus we get version b as well. Note that the array name alone always returns a reference to the start of the array. Once we factor out all the pointer arithmetic, we are essentially just returning the dereferenced value of an address within the array.

Version c is the conventional notation using the subscript operator. However the subscript operator is merely sugar-coating (a notational convenience). Behind the scenes, we're actually executing a.

Version d is an unusual form that is rarely seen but is perfectly valid. Just as str[4] is functionally equivalent to *(str+4), so 4[str] is functionally equivalent to *(4+str), thus d is the same as b (which is the same as a).

Versions e and f are similar to a and b respectively, but instead of taking the address of the start of the array, we now take the address of the 3rd element (index 2) and then offset by 2 elements to get to the 5th element. In these versions, we can also use a negative offset to move backwards from an element, so long as we remain within the bounds of the array. These versions can also be notated more conventionally using version c; str[2+2] which is the same as a.

Version g is similar to e except we now store the address of the 7th element (index 6) in p and then use pointer arithmetic to obtain the offset of the 5th element. However, this version is not commutative so we cannot use *(2-p), but is functionally the same as a.

There are other variants similar to these 7 but they all ultimately come back to version a once you factor out all the pointer arithmetic. In other words, no method is better or worse than any other method. However version c is favoured for its readability alone, regardless of whether the offset is a compile-time constant or needs to be calculated at runtime. All others are academic but give a better understanding of what's really going on behind the scenes.

List out primary goals of java Technology?

There were five primary goals in the creation of the Java language:

1. It should be "simple, object oriented, and familiar".

2. It should be "robust and secure".

3. It should be "architecture neutral and portable".

4. It should execute with "high performance".

5. It should be "interpreted, threaded, and dynamic".

What is base class pointer?

It is exactly what it says it is: a pointer to a base class. The assumption is that you have an object to a derived class, but actually hold a pointer to its base class. The following minimal example demonstrates this:

class base{ public: virtual ~base(){} };

class derived: public base{};

int main()

{

base* p = new derived; // base class pointer to a derived instance.

delete( p );

return(0);

}

Note that derived has an "is-a" relationship with base (derived is a base), thus the above code is perfectly legal. Moreover, because the base class destructor is declared virtual, when you delete p you automatically destroy the instance of derived before the instance of base, thus ensuring a clean teardown (without a virtual destructor, a dangling reference to derived would be left behind, which will only lead to problems further down the line).

Taking things further, calling any virtual methods upon the base class automatically invokes the override in your derived class, thus ensuring that your derived class behaves accordingly, polymorphically, even though you only hold a pointer to the base class. In other words, the base class provides a generic interface that is common to all its derivatives, and you can call those generic methods via the base class pointer without ever needing to know the actual derived type.

Remember that base classes should never know anything about their derivatives since a new derivative could be created at any time in the future and would therefore be impossible to predict in advance. But so long as the derivative makes use of the virtual functions (the generic interface) provided by the base class, there is never any need to know the actual type. The derivative's own v-table takes care of that for you, thus completely eliminating the need for expensive runtime type information and dynamic downcasts (which is always a sign of poor class design). This then makes it possible for your derived class overrides to call non-generic methods, thus extorting non-generic behaviour from what is essentially a generic, base class pointer.

Scientific calculator in java source code?

The following is for a simple calculator; a scientific calculator is more complicated. I will assume the user will press keys to have a number appear on the display; to (for example) add this with another number, the user presses "+" followed by the second number, followed by "=".

You would use a visual interface, with the number display, the digits, the equal sign, and a few operations (plus, minus, multiply, divide, ...).

As soon as the user types the second number, the first number will disappear from the screen; therefore, you have to save it into some variable.

Every time a key is pressed, you have to decide whether you should add a digit to the number in the display (if it is a digit, or a period), or whether to do a calculation (if it is +, -, *, /). If it is an operation, do the operation (conditionally, depending on the selected operation), and replace the number displayed by the new value.

This is my sample code using JAVA:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.text.*;

import javax.swing.border.*;

class Calc extends JFrame implements ActionListener

{

private static final Font BIGGER_FONT = new Font("Tahoma", Font.BOLD, 13);

private JRadioButton decimalb,binaryb,octalb,hexab,enableb, disableb;

private JTextField _displayField, decimalTF, binaryTF, octalTF, hexaTF;

private JLabel decimalL,binaryL,octalL,hexaL;

private JButton exitb, convertb, clearb, backSpace, b9, b8, b7, b6, b5, b4, b3, b2, b1, b0, b11, b12, b13;

private JButton b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25;

private JPanel cbPanel, upperButtonPanel;

private ButtonGroup group, group2;

private boolean pluz = false;

private boolean subs = false;

private boolean multiply = false;

private boolean divide = false;

private boolean _startNumber = true;

private boolean dotCheck = false;

private String firstValue, Secondvalue;

private String _previousOp = "=";

private DecimalFormat df = new DecimalFormat("###,###,###.##");

private CalculatorLogic calLogic = new CalculatorLogic();

public static void main(String[] args) {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (Exception unused) {

;

}

Calc window = new Calc();

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setVisible(true);

}

public Calc()

{

super("Calculator With Number System Converter");

_displayField = new JTextField("", 15);

_displayField.setHorizontalAlignment(JTextField.RIGHT);

_displayField.setFont(BIGGER_FONT);

_displayField.setEditable(false);

_displayField.setBackground(Color.WHITE);

upperButtonPanel = new JPanel();

upperButtonPanel.setLayout(new FlowLayout());

upperButtonPanel.setBackground(Color.lightGray);

upperButtonPanel.setPreferredSize(new Dimension(170, 300));

decimalb = new JRadioButton("Decimal", false);

binaryb = new JRadioButton("Binary", false);

octalb = new JRadioButton("Octal", false);

hexab = new JRadioButton("Hexadecimal", false);

decimalb.setFont(BIGGER_FONT);

binaryb.setFont(BIGGER_FONT);

octalb.setFont(BIGGER_FONT);

hexab.setFont(BIGGER_FONT);

decimalb.addActionListener(this);

binaryb.addActionListener(this);

octalb.addActionListener(this);

hexab.addActionListener(this);

group = new ButtonGroup();

group.add(decimalb);

group.add(binaryb);

group.add(octalb);

group.add(hexab);

upperButtonPanel.add(decimalb);

upperButtonPanel.add(binaryb);

upperButtonPanel.add(octalb);

upperButtonPanel.add(hexab);

upperButtonPanel.setVisible(false);

JPanel panel = new JPanel(new GridLayout(10, 1));

panel.setPreferredSize(new Dimension(300, 300));

panel.setBorder(new BevelBorder(BevelBorder.LOWERED));

decimalTF = new JTextField(20);

binaryTF = new JTextField(20);

octalTF = new JTextField(20);

hexaTF = new JTextField(20);

decimalTF.setEditable(false);

binaryTF.setEditable(false);

octalTF.setEditable(false);

hexaTF.setEditable(false);

decimalTF.setBackground(Color.WHITE);

binaryTF.setBackground(Color.WHITE);

octalTF.setBackground(Color.WHITE);

hexaTF.setBackground(Color.WHITE);

decimalTF.setFont(BIGGER_FONT);

binaryTF.setFont(BIGGER_FONT);

octalTF.setFont(BIGGER_FONT);

hexaTF.setFont(BIGGER_FONT);

decimalTF.setVisible(false);

binaryTF.setVisible(false);

octalTF.setVisible(false);

hexaTF.setVisible(false);

decimalL = new JLabel("Decimal Value");

binaryL = new JLabel("Binary Value");

octalL = new JLabel("Octal Value");

hexaL = new JLabel("Hexadecimal Value");

decimalL.setFont(BIGGER_FONT);

binaryL.setFont(BIGGER_FONT);

octalL.setFont(BIGGER_FONT);

hexaL.setFont(BIGGER_FONT);

decimalL.setVisible(false);

binaryL.setVisible(false);

octalL.setVisible(false);

hexaL.setVisible(false);

panel.add(decimalL);

panel.add(decimalTF);

panel.add(binaryL);

panel.add(binaryTF);

panel.add(octalL);

panel.add(octalTF);

panel.add(hexaL);

panel.add(hexaTF);

upperButtonPanel.add(panel);

add(upperButtonPanel, BorderLayout.NORTH);

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new GridLayout(5, 2, 5, 5));

buttonPanel.setBackground(Color.lightGray);

buttonPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));

b9 = new JButton("9");

b8 = new JButton("8");

b7 = new JButton("7");

b6 = new JButton("6");

b5 = new JButton("5");

b4 = new JButton("4");

b3 = new JButton("3");

b2 = new JButton("2");

b1 = new JButton("1");

b0 = new JButton("0");

b11 = new JButton(".");

b12 = new JButton("/");

b13 = new JButton("*");

b14 = new JButton("-");

b15 = new JButton("+");

b16 = new JButton("A");

b17 = new JButton("B");

b18 = new JButton("C");

b19 = new JButton("D");

b20 = new JButton("E");

b21 = new JButton("F");

b22 = new JButton("=");

b23 = new JButton("sqrt");

b24 = new JButton("x\u00B2");

b25 = new JButton("x\u00B3");

b1.setFont(BIGGER_FONT);

b2.setFont(BIGGER_FONT);

b3.setFont(BIGGER_FONT);

b4.setFont(BIGGER_FONT);

b5.setFont(BIGGER_FONT);

b6.setFont(BIGGER_FONT);

b7.setFont(BIGGER_FONT);

b8.setFont(BIGGER_FONT);

b9.setFont(BIGGER_FONT);

b0.setFont(BIGGER_FONT);

b11.setFont(BIGGER_FONT);

b12.setFont(BIGGER_FONT);

b13.setFont(BIGGER_FONT);

b14.setFont(BIGGER_FONT);

b15.setFont(BIGGER_FONT);

b16.setFont(BIGGER_FONT);

b17.setFont(BIGGER_FONT);

b18.setFont(BIGGER_FONT);

b19.setFont(BIGGER_FONT);

b20.setFont(BIGGER_FONT);

b21.setFont(BIGGER_FONT);

b22.setFont(BIGGER_FONT);

b23.setFont(BIGGER_FONT);

b24.setFont(BIGGER_FONT);

b25.setFont(BIGGER_FONT);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

b7.addActionListener(this);

b8.addActionListener(this);

b9.addActionListener(this);

b0.addActionListener(this);

b11.addActionListener(this);

b12.addActionListener(this);

b13.addActionListener(this);

b14.addActionListener(this);

b15.addActionListener(this);

b16.addActionListener(this);

b17.addActionListener(this);

b18.addActionListener(this);

b19.addActionListener(this);

b20.addActionListener(this);

b21.addActionListener(this);

b22.addActionListener(this);

b23.addActionListener(this);

b24.addActionListener(this);

b25.addActionListener(this);

b16.setEnabled(false);

b17.setEnabled(false);

b18.setEnabled(false);

b19.setEnabled(false);

b20.setEnabled(false);

b21.setEnabled(false);

buttonPanel.add(b7);

buttonPanel.add(b8);

buttonPanel.add(b9);

buttonPanel.add(b12);

buttonPanel.add(b13);

buttonPanel.add(b15);

buttonPanel.add(b4);

buttonPanel.add(b5);

buttonPanel.add(b6);

buttonPanel.add(b15);

buttonPanel.add(b14);

buttonPanel.add(b11);

buttonPanel.add(b1);

buttonPanel.add(b2);

buttonPanel.add(b3);

buttonPanel.add(b0);

buttonPanel.add(b22);

buttonPanel.add(b16);

buttonPanel.add(b17);

buttonPanel.add(b18);

buttonPanel.add(b11);

buttonPanel.add(b23);

buttonPanel.add(b19);

buttonPanel.add(b20);

buttonPanel.add(b21);

buttonPanel.add(b24);

buttonPanel.add(b25);

add(buttonPanel);

group2 = new ButtonGroup();

disableb = new JRadioButton("Disable Converter", true);

enableb = new JRadioButton("Enable Converter", false);

disableb.setFont(BIGGER_FONT);

enableb.setFont(BIGGER_FONT);

disableb.addActionListener(this);

enableb.addActionListener(this);

group2.add(disableb);

group2.add(enableb);

cbPanel = new JPanel(new GridLayout(3, 3));

cbPanel.setBorder(new BevelBorder(BevelBorder.RAISED));

cbPanel.add(disableb);

cbPanel.add(enableb);

convertb = new JButton("Convert");

clearb = new JButton("Clear");

exitb = new JButton("Exit");

backSpace = new JButton("BackSpace");

convertb.addActionListener(this);

clearb.addActionListener(this);

exitb.addActionListener(this);

backSpace.addActionListener(this);

convertb.setFont(BIGGER_FONT);

clearb.setFont(BIGGER_FONT);

exitb.setFont(BIGGER_FONT);

backSpace.setFont(BIGGER_FONT);

cbPanel.add(convertb);

cbPanel.add(clearb);

cbPanel.add(backSpace);

cbPanel.add(exitb);

convertb.setEnabled(false);

JPanel content2 = new JPanel();

content2.setLayout(new BorderLayout(5, 5));

content2.add(_displayField, BorderLayout.NORTH);

content2.add(buttonPanel , BorderLayout.CENTER);

content2.add(cbPanel, BorderLayout.SOUTH);

add(content2, BorderLayout.SOUTH);

this.setSize(340, 300);

this.setResizable(false);

this.setLocationRelativeTo(null);

}

public void actionPerformed(ActionEvent e)

{

try{

if(decimalb.isSelected())

{

b0.setEnabled(true);

b1.setEnabled(true);

b2.setEnabled(true);

b3.setEnabled(true);

b4.setEnabled(true);

b5.setEnabled(true);

b6.setEnabled(true);

b7.setEnabled(true);

b8.setEnabled(true);

b9.setEnabled(true);

b16.setEnabled(false);

b17.setEnabled(false);

b18.setEnabled(false);

b19.setEnabled(false);

b20.setEnabled(false);

b21.setEnabled(false);

}

else if(binaryb.isSelected())

{

b2.setEnabled(false);

b3.setEnabled(false);

b4.setEnabled(false);

b5.setEnabled(false);

b6.setEnabled(false);

b7.setEnabled(false);

b8.setEnabled(false);

b9.setEnabled(false);

b16.setEnabled(false);

b17.setEnabled(false);

b18.setEnabled(false);

b19.setEnabled(false);

b20.setEnabled(false);

b21.setEnabled(false);

}

else if(octalb.isSelected())

{

b0.setEnabled(true);

b1.setEnabled(true);

b2.setEnabled(true);

b3.setEnabled(true);

b4.setEnabled(true);

b5.setEnabled(true);

b6.setEnabled(true);

b7.setEnabled(true);

b16.setEnabled(false);

b17.setEnabled(false);

b18.setEnabled(false);

b19.setEnabled(false);

b20.setEnabled(false);

b21.setEnabled(false);

b8.setEnabled(false);

b9.setEnabled(false);

}

else if(hexab.isSelected())

{

b0.setEnabled(true);

b1.setEnabled(true);

b2.setEnabled(true);

b3.setEnabled(true);

b4.setEnabled(true);

b5.setEnabled(true);

b6.setEnabled(true);

b7.setEnabled(true);

b8.setEnabled(true);

b9.setEnabled(true);

b16.setEnabled(true);

b17.setEnabled(true);

b18.setEnabled(true);

b19.setEnabled(true);

b20.setEnabled(true);

b21.setEnabled(true);

}

if((e.getActionCommand().equals("Convert")) && decimalb.isSelected())

{

String input, bin="",oct="",hex="";

int rem,rem2,rem3,len;

char x;

input = _displayField.getText().trim();

try{

if(input.equals(""))

throw new Exception("Error");

else

len = input.length();

int num = Integer.parseInt(input);

while(num>0)

{

rem = num%2;

num/=2;

bin = rem + bin;

}

binaryTF.setText(bin);

decimalTF.setText(input);

int num2 = Integer.parseInt(input);

while(num2>0)

{

rem2 = num2%8;

oct = rem2 + oct;

num2/=8;

}

octalTF.setText(oct);

int num3 = Integer.parseInt(input);

while(num3>0)

{

rem3 = num3%16;

if(rem3<10)

hex = rem3 + hex;

else if(rem3 0)

{

JOptionPane.showMessageDialog(null,"Can't divide by zero", "Error Message", JOptionPane.ERROR_MESSAGE);

return 0;

}

else

return (a / b);

}

public double stringToDouble(String a)

{

return Double.parseDouble(a);

}

}

Explain about exception handling with multiple exception?

Exception handling in Java is done through a try-catch-finally block. The "try" block of code is where you put the code which may throw an exception. The "catch" block of code is where you put the code which will execute after an exception is thrown in the try block. This is often used to display an error message, or to mitigate problems caused by the exception. The "finally" block is where you put code that you want to execute after the try and catch blocks have been processed.

// example code for catching exception while reading from a file

try {

// this line of code can throw a FileNotFoundException

FileReader in = new FileReader("myfile.txt");

// this line of code can throw an IOException

in.read();

} catch(FileNotFoundException ex) { // catch first exception type

System.err.println("Cannot find myfile.txt!");

} catch(IOException ex) { //catch second exception type

System.err.println("Cannot read from myfile.txt!");

} finally { // no matter what we want to close the file, so do it here

// however, this line can also cause an exception, so we need to catch that, too

try {

in.close();

catch(IOException ex) {

// not much we can do about an exception that occurs here

}

}

Why is Java known as platform neutral language and how is Java more secured than other language justify?

"Platform neutral" means that Java does not depend directly on the underlying operating system; the same binary Java classes will run on any platform that support the target version of Java that those classes use, such as Windows, Linux, and Mac OS.

Java has enhanced security opposed to a native binary (e.g. a program written in C++) in the sense that an additional layer of security is implemented in the Java VM that reduces promiscuous behavior (such as accessing the hard drive when running as an applet). Java is arguably not as secure as some other languages (such as Mono/.NET, which can be statically verified safe without executing the program), but it is generally more secure than non-VM-based languages because of the extra layer of security.

What is servlet cookies in java programming?

a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management.

A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. Some Web browsers have bugs in how they handle the optional attributes, so use them sparingly to improve the interoperability of your servlets.

The servlet sends cookies to the browser by using the HttpServletResponse.addCookie(javax.servlet.http.Cookie) method, which adds fields to HTTP response headers to send cookies to the browser, one at a time. The browser is expected to support 20 cookies for each Web server, 300 cookies total, and may limit cookie size to 4 KB each.

The browser returns cookies to the servlet by adding fields to HTTP request headers. Cookies can be retrieved from a request by using the HttpServletRequest.getCookies() method. Several cookies might have the same name but different path attributes.

Cookies affect the caching of the Web pages that use them. HTTP 1.0 does not cache pages that use cookies created with this class. This class does not support the cache control defined with HTTP 1.1.

What will happen if a String array is not provided as the argument to the main method in Java EE?

java takes command line arguments from user so as to execute the program,that's why we always mention arguments for main() , now java always assumes that the argumetns are of String type which may be appropriately converted to any other type later. that is why we pass string array to hold the command line argumetns supplied by the user.

Who was an early kingdom centered on the island of java and Bali?

Borobudur was an early kingdom centered on the island of java and Bali.

Can we support java in HTC desire 620?

HTC 620 uses 4.4 android which partially supports java but it does not run oracle JVM or even oracle java.

Which of the class in java.io package defines a method to delete a file?

The java.io.File class in Java has a delete() method to delete the file.

See related link for details.