Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Standard Way To Use OOP Paradigm: Main Class

This document contains code examples demonstrating different object-oriented programming concepts in Java: 1) It shows a basic calculator program with a main class that creates an instance of a calculator logic class, which takes user input for two numbers and an operation and performs the calculation. 2) It demonstrates a generic class that can hold different data types by creating instances with an Integer and a String. 3) It includes code for a GUI application that saves user input bio data to a text file on button click by getting the text from JTextFields and writing bytes to an output stream.

Uploaded by

Khalid Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Standard Way To Use OOP Paradigm: Main Class

This document contains code examples demonstrating different object-oriented programming concepts in Java: 1) It shows a basic calculator program with a main class that creates an instance of a calculator logic class, which takes user input for two numbers and an operation and performs the calculation. 2) It demonstrates a generic class that can hold different data types by creating instances with an Integer and a String. 3) It includes code for a GUI application that saves user input bio data to a text file on button click by getting the text from JTextFields and writing bytes to an output stream.

Uploaded by

Khalid Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Standard Way to use OOP Paradigm

Main Class
public class calclass {
public static void main(String args[])
{
calculator C1 = new calculator();
C1.operation();
}
}

Logic Class
import java.util.Scanner;
public class calculator {
private double num1,num2, ans;
private String op;
Scanner input = new Scanner(System.in);
public calculator()
{
System.out.println("CALCULATOR");
System.out.println("Enter First Number");
num1 = input.nextInt();
System.out.println("Enter Second Number");
num2 = input.nextInt();
System.out.println("Enter Operand");
op = input.next();
}
public void operation()
{
switch (op){
case"+":
{
Addition();
}break;
case "-":
{
Subtraction();
}break;

case"*":
{
Multiplication();
}break;
case"/":
{
Division();
}break;
default:
{
System.out.println("Wrong operand");
}break;
}
}
public void Addition()
{
ans = num1 + num2;
System.out.println("The Answer is :" + ans);
}
public void Subtraction()
{
ans = num1 - num2;
System.out.println("The Answer is :" + ans);
}
public void Multiplication()
{
ans = num1 * num2;
System.out.println("The Answer is :" + ans);
}
public void Division()
{
ans = num1 / num2;
System.out.println("The Answer is :" + ans);
}}

Generic Class

Main Class
package genericexample;
public class NormalCalss
{
private int id;
public void setID(int id)
{
this.id = id;
}
public int getID()
{
return this.id;
}
}

Logic Class
package genericexample;
public class GenericExample
{
public static void main(String[] args)
{
NormalCalss n = new NormalCalss();
n.setID(101);
System.out.println(n.getID());
GenericClass<Integer> g = new GenericClass<Integer>();
g.setId(1000);
System.out.println(g.getId());
GenericClass<String> g1 = new GenericClass<String>();
g1.setId("Name");
}
}

BioData Form Through Filing (GUI)


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

System.exit(0);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{ Date d = new Date();
SimpleDateFormat Dateformat = new SimpleDateFormat("dd.MMM.yyyy
hh.mm.ss");
try{
FileOutputStream out = new
FileOutputStream("D://"+Dateformat.format(d)+txtName.getText()+".txt");
String sData;
byte[] bData;
//----------------------------sData = "Name is: " + txtName.getText() + "\r\n";
bData = sData.getBytes();
out.write(bData);
//----------------------------sData = "Age is: " + txtAge.getText() + "\r\n";
bData = sData.getBytes();
out.write(bData);
//----------------------------sData = "Phone No: "+ txtPhone.getText() + "\r\n";
bData = sData.getBytes();
out.write(bData);
//----------------------------sData = "CNIC No : " + txtCNIC.getText() + "\r\n";
bData = sData.getBytes();
out.write(bData);
out.close();
JOptionPane.showMessageDialog(this, "Data Save Successfully!!");
txtAge.setText("");
txtCNIC.setText("");
txtName.setText("");
txtPhone.setText("");
}
catch(Exception ex){
JOptionPane.showMessageDialog(this, ex);
}}

You might also like