Java examples of each and defeintions 5 Strings Basic method
Java
examples of each and defeintions
5. Strings Basic methods and operations.
6. Math Hint: random()
7. Scanner Demo the Scanner class.
8. JOptionPane Demo the JOptionPane class.
10. Conditionals
12. Conditional Operators
13. Bits Use of & | ^ ~ << >> operators and how to set/unset bits.
17. Classes
Solution
5. In java programming strings are widely used as a sequence of characters, The strings are threads as objects.
Ex:
public class StringDemo {
public static void main(String args[]) {
char[] helloArray = { \'h\', \'e\', \'l\', \'l\', \'o\', \'.\' };
String helloString = new String(helloArray);
System.out.println( helloString );
}
}
In order to find the string length:
public class StringDemo {
public static void main(String args[]) {
String palindrome = \"Dot saw I was Tod\";
int len = palindrome.length();
System.out.println( \"String Length is : \" + len );
}
}
String methods and operations:
int compareTo(Object o):Compares this String to another Object.
String concat(String str):Concatenates the specified string to the end of this string.
static String copyValueOf(char[] data):Returns a String that represents the character sequence in the array specified.
boolean equals(Object anObject):Compares this string to the specified object.
int hashCode():Returns a hash code for this string.
int indexOf(String str):Returns the index within this string of the first occurrence of the specified substring.
6. The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.
Ex:
// Returns a random integer between min (included) and max (excluded)
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
7. The Java Scanner class breaks the input into tokens using a delimiter that is whitespace bydefault. It provides many methods to read and parse various primitive values.
Ex:
import java.util.Scanner;
class ScannerTest{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println(\"Enter your rollno\");
int rollno=sc.nextInt();
System.out.println(\"Enter your name\");
String name=sc.next();
System.out.println(\"Enter your fee\");
double fee=sc.nextDouble();
System.out.println(\"Rollno:\"+rollno+\" name:\"+name+\" fee:\"+fee);
sc.close();
}
}
8. The class JOptionPane is a component which provides standard methods to pop up a standard dialog box for a value or informs user of something.
Ex:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingControlDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingControlDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingControlDemo swingControlDemo = new SwingControlDemo();
swingControlDemo.showDialogDemo();
}
private void prepareGUI(){
mainFrame = new JFrame(\"Java Swing Examples\");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel(\"\", JLabel.CENTER);
statusLabel = new JLabel(\"\",JLabel.CENTER);
statusLabel.setSize(350,100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showDialogDemo(){
headerLabel.setText(\"Control in action: JOptionPane\");
JButton okButton = new JButton(\"OK\");
JButton javaButton = new JButton(\"Yes/No\");
JButton cancelButton = new JButton(\"Yes/No/Cancel\");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(
mainFrame, \"Welcome to TutorialsPoint.com\");
}
});
javaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int output = JOptionPane.showConfirmDialog(mainFrame
, \"Click any button\"
,\"TutorialsPoint.com\"
,JOptionPane.YES_NO_OPTION);
if(output == JOptionPane.YES_OPTION){
statusLabel.setText(\"Yes selected.\");
}else if(output == JOptionPane.NO_OPTION){
statusLabel.setText(\"No selected.\");
}
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int output = JOptionPane.showConfirmDialog(mainFrame
, \"Click any button\"
,\"TutorialsPoint.com\"
,JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if(output == JOptionPane.YES_OPTION){
statusLabel.setText(\"Yes selected.\");
}else if(output == JOptionPane.NO_OPTION){
statusLabel.setText(\"No selected.\");
}else if(output == JOptionPane.CANCEL_OPTION){
statusLabel.setText(\"Cancel selected.\");
}
}
});
controlPanel.add(okButton);
controlPanel.add(javaButton);
controlPanel.add(cancelButton);
mainFrame.setVisible(true);
}
}
10. Java uses boolean variables to evaluate conditions. The boolean values true and false are returned when an expression is compared or evaluated.
For example:
Int a = 4;
Boolean b = a == 4;
If (b)
{
System.out.println(“It’s true!”)
}
12.
The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit \"short-circuiting\" behavior, which means that the second operand is evaluated only if needed.
&& Conditional-AND
|| Conditional-OR
Ex:
class ConditionalDemo1 {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
if((value1 == 1) && (value2 == 2))
System.out.println(\"value1 is 1 AND value2 is 2\");
if((value1 == 1) || (value2 == 1))
System.out.println(\"value1 is 1 OR value2 is 1\");
}
}
13.
To set a bit, use:
x |= 0b1; // set LSB bit
x |= 0b10; // set 2nd bit from LSB.
to erase a bit use:
x &= ~0b1; // unset LSB bit (if set)
x &= ~0b10; // unset 2nd bit from LSB.
to toggle a bit use:
x ^= 0b1;
To dynamically set at bit, use:
x |= (1 << y); // set the yth bit from the LSB.
17. A class is a group of objects that has common properties. It is a template or blueprint from which objects are created.
Ex:
class Student1{
int id;//data member (also instance variable)
String name;//data member(also instance variable)
public static void main(String args[]){
Student1 s1=new Student1();//creating an object of Student
System.out.println(s1.id);
System.out.println(s1.name);
}
}






