Java programming from my class works- java pro 1
This for Modulus example
// Demonstrate the % operator.
class Modulus {
public static void main( String args []) {
int x = 42;
double y = 42.3;
[Link]("x mod 10 =" + x % 10);
[Link]( "y mod 10 =" + y % 10);
}
}
This is for OpEquals Example
class OpEquals { // Demonstate several assignment operators.
public static void main( String args []) {
int a = 1, b = 2, c = 3;
a+=5; b*=4; c+= a*b; c%=6;
[Link]("a =" + a); // a=1+5 = 6
[Link]("b =" + b); // b=2*4=8
[Link]("c =" + c); // c = 3+ (6*8)=51=> c%6 = 51%6=3
}
}
This is class Room example
class room
{
public static void main(String[] args)
{
int length=15;
int width=25;
int area = 375;
[Link]("The floor space is " + area + " square feet
");
}
}
This interest Class example
class interest
{
public static void main(String[] args)
{
double invested = 1000;
double rate=0.05;
double time=1;
[Link]("Future Amount" + invested * rate * time);
}
}
Changing Fahrenheit to Centigrade
class FahrenheitToCentigrade
{
public static void main(String[] args)
{
int hbt;
int result;
int temp;
temp = 0;
hbt = 100;
temp = hbt - 32;
result = temp*5/9;
[Link]("Result = " + result);
}
}
class Circle
{
public static void main(String[] args)
{
int radius;
double PI;
double result;
radius = 3;
PI = 3.14;
circle area = 1/2 PI;
result = temp*5/9;
[Link]("Result = " + result);
}
}
Class Time
class time
{
public static void main(String[] args)
{
int minutes= 197;
[Link]("197 minutes becomes 3 hours and 17 minutes")
;
}
}
Class Try to parse string
public class TryToParseString {
public static void main(String[] args) {
int i;
String str = "a"; // change this to any string you want
try {
i = [Link] ( str ) ; // get the integer
[Link](i); // print out i
} catch ( NullPointerException npe ) {
[Link](str + " is not defined");
// if str is not defined
}
catch ( NumberFormatException nfe ) {
[Link](str + " is not a number");
// if str is not a number
}
}
}
Class BRReadLines
import [Link].*;// Read a string from console using a BufferedReader.
class BRReadLines{
public static void main(String[] args)throws IOException{
String str;
BufferedReader br=new BufferedReader(
new InputStreamReader([Link]));
[Link]("Enter lines of text.\n Enter 'stop' to quit.
");
do {
str=[Link]();
[Link](str);
} while ();
} }
Testing class
import [Link].*;
public class test {
public static void main(String[] args) {
String str = "";
try {
BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
[Link]( "> ");
str = [Link]();
} catch (IOException e) {
} // finished reading console input
try {
BufferedWriter out = new BufferedWriter(new FileWriter("[Link]"));
[Link](str);
[Link]();
} catch (IOException e) {
} // finished writing to a file
[Link]("Reading the file and printing .... ");
try {
BufferedReader in = new BufferedReader(new FileReader("[Link]"));
String s;
while ((s = [Link]()) != null) {
[Link](s);
}
[Link]();
} catch (IOException e){
} // finished reading the file and printing to std out
}
}
Simple java programming examples
class Win
{
public static void main(String[] args)
{
for (int i=0;i<10;i++)
{
[Link]("Hello! HAHAHAHAHA! You can do it !! "+i);
}
}
}
class Win1
{
public static void main(String[] args)
{
int MyArray[]={10, 11, 34,45};
for (int i=0;i<4;i++)
{
[Link](MyArray[i]);
}
}
}
class Win2
{
public static void main(String[] args)
{
int ArrayA[][]={{10, 11}, {34,45}};
int ArrayB[][]={{10, 11}, {34,45}};
int ArrayC[][];
//perform addition
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
ArrayC[i][j] = ArrayA[i][j]+ArrayB[i][j];
}
}
//display result
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
[Link](MyArray[i][j]+" ");
}
[Link]("\n");
}
}
}
Java programming from my class work- java pro 2
import [Link].*;
import [Link].*;
public class Printer extends JFrame {
public Printer(){
super("Printer");
setLayout(new BorderLayout());
//Label Printer: MyPrinter
JLabel labelMyPrinter = new JLabel("Printer: MyPrinter");
add(labelMyPrinter, BorderLayout.PAGE_START);
//Checkboxes
JCheckBox checkImage = new JCheckBox("Image");
JCheckBox checkText = new JCheckBox("Text");
CheckBox checkCode = new JCheckBox("Code");
JPanel panelCheck = new JPanel(new GridLayout(0, 1));
[Link](checkImage);
[Link](checkText);
[Link](checkCode);
//Radio buttons
JRadioButton radioSelection = new JRadioButton("Selection");
JRadioButton radioAll = new JRadioButton("All");
JRadioButton radioApplet = new JRadioButton("Applet");
JPanel panelRadio = new JPanel(new GridLayout(0, 1));
[Link](radioSelection);
[Link](radioAll);
[Link](radioApplet);
//Buttons
JButton buttonOk = new JButton("OK");
JButton buttonCancel = new JButton("Cancel");
JButton buttonSetup = new JButton("Setup...");
JButton buttonHelp = new JButton("Help");
JPanel panelButton = new JPanel(new GridLayout(0, 1));
[Link](buttonOk);
[Link](buttonCancel);
[Link](buttonSetup);
[Link](buttonHelp);
//Add Checkbox, radio button and button panel to middle panel
JPanel panelMiddle = new JPanel();
[Link](panelCheck);
[Link](panelRadio);
[Link](panelButton);
add(panelMiddle, [Link]);
//Print quality and print to file
JLabel labelQuality = new JLabel("Print Quality:");
String[] qualityOptions = {"High","Medium","Low"};
JComboBox comboQuality = new JComboBox(qualityOptions);
JCheckBox checkFile = new JCheckBox("Print to File:");
JPanel panelPrintFile = new JPanel();
[Link](labelQuality);
[Link](comboQuality);
[Link](checkFile);
add(panelPrintFile, BorderLayout.PAGE_END);
}}
Testing printer
import [Link];
public class PrinterTest {
public static void main(String[] args){
Printer printer = new Printer();
[Link]( JFrame.EXIT_ON_CLOSE);
[Link](500,300);
[Link](true);
}}
Second
import [Link].*;
import [Link].*;
import [Link].*;
public class TempConv extends JFrame implements ActionListener{
double fahrenheit, celsius;
JTextField textFahrenheit = new JTextField(10);
JButton buttonConvert = new JButton("Convert");
JLabel labelResult = new JLabel("Result:");
public TempConv(){
super("Fahrenheit to Celsius");
setLayout(new BorderLayout());
//Explanation
JLabel labelExplanation = new JLabel();
[Link]("<html>Please enter the value in Fahrenheit and press t
he Convert button to display the degrees</html>");
add(labelExplanation, BorderLayout.PAGE_START);
//Textarea and button
[Link](this);
JPanel panelInput = new JPanel(new FlowLayout());
[Link](textFahrenheit);
[Link](buttonConvert);
add(panelInput, [Link]);
//Result label
add(labelResult, BorderLayout.PAGE_END);
}
//Conversion
public void actionPerformed(ActionEvent e) {
double celsius;
double fahrenheit = [Link]([Link]());
celsius=(fahrenheit-32)*5/9;
[Link]("Result: "+celsius+" degree celsius");
}}
Testing temp
import [Link];
public class TempConvTest {
public static void main(String[] args){
TempConv tempConv = new TempConv();
[Link]( JFrame.EXIT_ON_CLOSE);
[Link](320,130);
[Link](true);
}}
Grid class
import [Link];
import [Link];
public class Grid extends JPanel
{public void paint(Graphics g)
{[Link](g);
for(int i=1;i<20;i++)
{[Link](20,20*i,380,20*i);
[Link](20*i,20,20*i,380);
} } }
Testing grid
import [Link];
public class TestGrid
{public static void main (String args[])
{JFrame frame = new JFrame ("Grid");
[Link](JFrame.EXIT_ON_CLOSE);
Grid RectJPanel = new Grid();
[Link](RectJPanel);
[Link](400,400);
[Link](true);
} }
Showing color
// Fig. 12.6: [Link]
// Demonstrating Colors.
import [Link];
public class ShowColors
{
// execute application
public static void main( String args[] )
{
// create frame for ColorJPanel
JFrame frame = new JFrame( "Using colors" );
[Link]( JFrame.EXIT_ON_CLOSE );
ColorJPanel colorJPanel = new ColorJPanel(); // create ColorJPanel
[Link]( colorJPanel ); // add colorJPanel to frame
[Link]( 400, 180 ); // set frame size
[Link]( true ); // displayt frame
} // end main
} // end class ShowColors
Draw Arcs
// Fig. 12.25: [Link]
// Drawing arcs.
import [Link];
public class DrawArcs
{
// execute application
public static void main( String args[] )
{
// create frame for ArcsJPanel
JFrame frame = new JFrame( "Drawing Arcs" );
[Link]( JFrame.EXIT_ON_CLOSE );
ArcsJPanel arcsJPanel = new ArcsJPanel(); // create ArcsJPanel
[Link]( arcsJPanel ); // add arcsJPanel to frame
[Link]( 300, 210 ); // set frame size
[Link]( true ); // display frame
} // end main
} // end class DrawArcs
Draw polygons
// Fig. 12.28: [Link]
// Drawing polygons.
import [Link];
public class DrawPolygons
{
// execute application
public static void main( String args[] )
{
// create frame for PolygonsJPanel
JFrame frame = new JFrame( "Drawing Polygons" );
[Link]( JFrame.EXIT_ON_CLOSE );
PolygonsJPanel polygonsJPanel = new PolygonsJPanel();
[Link]( polygonsJPanel ); // add polygonsJPanel to frame
[Link]( 280, 270 ); // set frame size
[Link]( true ); // display frame
} // end main
} // end class DrawPolygons
File demonstration
// Fig. 14.4: [Link]
// Demonstrating the File class.
import [Link];
public class FileDemonstration {
// display information about file user specifies
public void analyzePath( String path ) {
// create File object based on user input
File name = new File( path );
if ( [Link]() ) // if name exists, output information about it
{
// display file (or directory) information
[Link](
"%s%s\n%s\n%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s",
[Link](), " exists",
( [Link]() ? "is a file" : "is not a file" ),
( [Link]() ? "is a directory" :
"is not a directory" ),
( [Link]() ? "is absolute path" :
"is not absolute path" ), "Last modified: ",
[Link](), "Length: ", [Link](),
"Path: ", [Link](), "Absolute path: ",
[Link](), "Parent: ", [Link]() );
if ( [Link]() ) // output directory listing
{
String directory[] = [Link]();
[Link]( "\n\nDirectory contents:\n" );
for ( String directoryName : directory )
[Link]( "%s\n", directoryName );
} // end else
} // end outer if
else // not file or directory, output error message
{
[Link]( "%s %s", path, "does not exist." );
} // end else
} // end method analyzePath
} // end class FileDemonstration
File demonstration test
// Fig. 14.5: [Link]
// Testing the FileDemonstration class.
import [Link];
public class FileDemonstrationTest
{
public static void main( String args[] )
{
Scanner input = new Scanner( [Link] );
FileDemonstration application = new FileDemonstration();
[Link]( "Enter file or directory name here: " );
[Link]( [Link]() );
} // end main
} // end class FileDemonstrationTest
Account record
// Fig. 14.6: [Link]
// A class that represents one record of information.
public class AccountRecord
{
private int account;
private String firstName;
private String lastName;
private double balance;
// no-argument constructor calls other constructor with default values
public AccountRecord()
{
this ( 0, "", "", 0.0 ); // call four-argument constructor
} // end no-argument AccountRecord constructor
// initialize a record
public AccountRecord( int acct, String first, String last, double bal )
{
setAccount( acct );
setFirstName( first );
setLastName( last );
setBalance( bal );
} // end four-argument AccountRecord constructor
// set account number
public void setAccount( int acct )
{
account = acct;
} // end method setAccount
// get account number
public int getAccount()
{
return account;
} // end method getAccount
// set first name
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName
// get first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last;
} // end method setLastName
// get last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set balance
public void setBalance( double bal )
{
balance = bal;
} // end method setBalance
// get balance
public double getBalance()
{
return balance;
} // end method getBalance
} // end class AccountRecord