0% found this document useful (0 votes)
5 views9 pages

Prime Digit Sum Calculator in Java

The document contains multiple Java programs demonstrating various functionalities, including prime digit summation, file operations (append, overwrite, read), a simple Swing calculator, and a database connection for student data entry. Each section illustrates different programming concepts such as exception handling, threading, file I/O, GUI creation, and database interaction. The overall focus is on practical applications of Java programming in different contexts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Prime Digit Sum Calculator in Java

The document contains multiple Java programs demonstrating various functionalities, including prime digit summation, file operations (append, overwrite, read), a simple Swing calculator, and a database connection for student data entry. Each section illustrates different programming concepts such as exception handling, threading, file I/O, GUI creation, and database interaction. The overall focus is on practical applications of Java programming in different contexts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

exception:

import [Link];​

public class PrimeDigitExceptionDemo {​

public static boolean isPrimeDigit(int d) {​
return d == 2 || d == 3 || d == 5 || d == 7;​
}​

public static void main(String[] args) {​

Scanner sc = new Scanner([Link]);​

[Link]("Enter your roll number (only digits): ");​
long roll = 0;​

try {​
roll = [Link](); // taking input as long​
} ​
catch (Exception e) {​
[Link]("Invalid input! Please enter only digits.");​
return; // stop program​
}​

int sum = 0;​

// extract digits using modulo​
while (roll > 0) {​
long digit = roll % 10; // last digit​
roll = roll / 10; // remove last digit​

if (isPrimeDigit((int)digit)) {​
sum += digit;​
}​
}​

[Link]("Sum of prime digits = " + sum);​
}​
}
2. Thread:

package [Link];​
import [Link];​
class PrimeDigitThread extends Thread {​
long roll;​
int sum;​

PrimeDigitThread(long roll) {​
[Link] = roll;​
}​
public void run() {​
sum = 0;​
long n = roll;​
while (n > 0) {​
int digit = (int)(n % 10);​
if (digit==2||digit==3||digit==5||digit==7) sum += digit;​
n /= 10;​
}​
}​
}​
public class PrimeDigitExceptionDemo {​
public static void main(String[] args) {​

Scanner sc = new Scanner([Link]);​
[Link]("Enter your roll number: ");​

long roll;​
try {​
roll = [Link]();​
} catch (Exception e) {​
[Link]("Invalid input! Only digits allowed.");​
return;​
}​

PrimeDigitThread t = new PrimeDigitThread(roll);​
[Link]();​
try { [Link](); } catch (InterruptedException e) {}​

[Link]("Sum of prime digits = " + [Link]);​
}​
}​
3. APpend file:

import [Link];​
import [Link];​
import [Link];​

public class PrimeDigitExceptionDemo {​
public static void main(String[] args) throws IOException {​

String str = "We are living in a society " +​
"where everything is delusional 2 " +​
"404";​

String dirname = "E:\\downloads\\Documents";​
String filename = "[Link]";​

File file = new File(dirname, filename);​

// Append the string directly​
FileWriter fw = new FileWriter(file, true); // true = append mode​
[Link](str + "\n"); // add new line automatically​
[Link]();​

[Link]("Writing successful!");​
}​
}

4. Normal write:

import [Link];​
import [Link];​
import [Link];​

public class PrimeDigitExceptionDemo{​
public static void main(String[] args) throws IOException {​

String str = "This will overwrite the file.\n" +​
"Old content will be removed!";​

String dirname = "E:\\downloads\\Documents";​
String filename = "[Link]";​

File file = new File(dirname, filename);​

// Write (overwrite) the file​
FileWriter fw = new FileWriter(file); // no 'true' → overwrite
mode​
[Link](str + "\n"); // write string​
[Link]();​

[Link]("File written (overwritten) successfully!");​
}​
}

4. Read:
package [Link];​

import [Link];​
import [Link];​
import [Link];​
import [Link];​

public class PrimeDigitExceptionDemo {​
public static void main(String[] args) throws IOException {​
int ch;​
String dirname = "E:\\downloads\\Documents";​
String filename = "[Link]";​
File file = new File(dirname, filename);​
FileReader fr = null;​
try {​
fr = new FileReader(file);​
} catch (FileNotFoundException e) {​
[Link]("File not found.");​
}​
if (fr != null) {​
while ((ch = [Link]()) != -1) {​
[Link]((char) ch);​
}​
[Link]();​
}​
}​
}
5. Swing Calculator:

package [Link];​

import [Link].*;​
import [Link].*;​

public class Jswing {​

public static void main(String[] args) {​

JFrame frame = new JFrame("Calculator");​
[Link](320, 350);​
[Link](JFrame.EXIT_ON_CLOSE);​
[Link](null); // ← NO LAYOUT​

// Text fields​
JTextField t1 = new JTextField();​
[Link](50, 30, 200, 30);​

JTextField t2 = new JTextField();​
[Link](50, 70, 200, 30);​

// Buttons​
JButton sumBtn = new JButton("Sum");​
[Link](30, 120, 100, 30);​

JButton subBtn = new JButton("Subtract");​
[Link](150, 120, 100, 30);​

JButton mulBtn = new JButton("Multiply");​
[Link](30, 160, 100, 30);​

JButton divBtn = new JButton("Divide");​
[Link](150, 160, 100, 30);​

// Result label​
JLabel resultLabel = new JLabel("Result:");​
[Link](50, 220, 200, 30);​

// Add components​
[Link](t1);​
[Link](t2);​
[Link](sumBtn);​
[Link](subBtn);​
[Link](mulBtn);​
[Link](divBtn);​
[Link](resultLabel);​

// SUM​
[Link](e -> {​
int a = [Link]([Link]());​
int b = [Link]([Link]());​
[Link]("Result: " + (a + b));​
});​

// SUBTRACT​
[Link](e -> {​
int a = [Link]([Link]());​
int b = [Link]([Link]());​
[Link]("Result: " + (a - b));​
});​

// MULTIPLY​
[Link](e -> {​
int a = [Link]([Link]());​
int b = [Link]([Link]());​
[Link]("Result: " + (a * b));​
});​

// DIVIDE (with decimal support)​
[Link](e -> {​
double a = [Link]([Link]());​
double b = [Link]([Link]());​

if (b == 0) {​
[Link]("Cannot divide by zero!");​
} else {​
[Link]("Result: " + (a / b));​
}​
});​

[Link](true);​
}​
}
6. Database connect:

package db;​

import [Link].*;​
import [Link].*;​
import [Link].*;​
import [Link].*;​

public class Db extends JFrame implements ActionListener {​

JTextField idField, nameField;​
JButton submitBtn;​

// Database connection info​
String url =
"jdbc:mysql://localhost:3306/student?zeroDateTimeBehavior=CONVERT_TO_NULL";​
String user = "root";​
String password = "";​

public Db() {​
// Frame setup​
setTitle("Student Data Entry");​
setLayout(new FlowLayout());​
setSize(300, 200);​
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);​

// Components​
add(new JLabel("Student ID:"));​
idField = new JTextField(15);​
add(idField);​

add(new JLabel("Student Name:"));​
nameField = new JTextField(15);​
add(nameField);​

submitBtn = new JButton("Submit");​
add(submitBtn);​

[Link](this);​

setVisible(true);​
}​

@Override​
public void actionPerformed(ActionEvent e) {​
String idText = [Link]();​
String name = [Link]();​

if ([Link]() || [Link]()) {​
[Link](this, "Please fill all fields!");​
return;​
}​

try {​
int id = [Link](idText);​

// Load driver​
[Link]("[Link]");​

// Connect to database​
Connection conn = [Link](url, user,
password);​

// Prepare SQL statement​
String query = "INSERT INTO student (id, name) VALUES (?, ?)";​
PreparedStatement ps = [Link](query);​
[Link](1, id);​
[Link](2, name);​

// Execute update​
int rows = [Link]();​

if (rows > 0) {​
[Link](this, "Data inserted
successfully!");​
[Link]("");​
[Link]("");​
}​

[Link]();​

} catch (NumberFormatException ex) {​
[Link](this, "ID must be a number!");​
} catch (SQLIntegrityConstraintViolationException ex) {​
[Link](this, "This ID already exists!");​
} catch (ClassNotFoundException ex) {​
[Link](this, "MySQL Driver not found!");​
} catch (SQLException ex) {​
[Link](this, "Database Error: " +
[Link]());​
}​
}​

public static void main(String[] args) {​
new Db();​
}​
}

You might also like