Skip to main content

Quine-McCluskey Tabular Method

CHS
1 like • Feb 3, 2021 • 0 views
Java
Loading...

More Java Posts

Add Binary Numbers

0 likes • Oct 15, 2022 • 0 views
Java
public static String addBinary(){
// The two input Strings, containing the binary representation of the two values:
String input0 = "1010";
String input1 = "10";
// Use as radix 2 because it's binary
int number0 = Integer.parseInt(input0, 2);
int number1 = Integer.parseInt(input1, 2);
int sum = number0 + number1;
return Integer.toBinaryString(sum); //returns the answer as a binary value;
}

Puzzle 1

0 likes • Feb 6, 2021 • 0 views
Java
import java.util.Scanner;
/* In Heaven, the seed is tall and the oak small / It's input vital
* "The al-Madinatian," verse 236-37
* tinyurl.com/17snqkfv
* email answer to [email protected]
**/
public class Main
{
public static void main(String args[])
{
int x = 0;
int a, c, m = 10000;
Scanner in = new Scanner(System.in);
System.out.println("Please enter a seed, maximum 3000000:");
x = in.nextInt();
System.out.println("Please input a number, maximum 29:");
a = in.nextInt();
System.out.println("Please input a number, maximum 999999:");
c = in.nextInt();
char menu = 'y';
while(menu != 'n')
{
x = (a*x + c) % m;
System.out.println("The next number is " + x + "\nWould you like another? (y/n)");
menu = in.next().charAt(0);
}
}
}
//██╗  ░█████╗░███╗░░░███╗  ██████╗░░█████╗░███████╗██████╗░░█████╗░██╗░░░░░██╗░░░██╗░██████╗
//██║  ██╔══██╗████╗░████║  ██╔══██╗██╔══██╗██╔════╝██╔══██╗██╔══██╗██║░░░░░██║░░░██║██╔════╝
//██║  ███████║██╔████╔██║  ██║░░██║███████║█████╗░░██║░░██║███████║██║░░░░░██║░░░██║╚█████╗░
//██║  ██╔══██║██║╚██╔╝██║  ██║░░██║██╔══██║██╔══╝░░██║░░██║██╔══██║██║░░░░░██║░░░██║░╚═══██╗
//██║  ██║░░██║██║░╚═╝░██║  ██████╔╝██║░░██║███████╗██████╔╝██║░░██║███████╗╚██████╔╝██████╔╝
//╚═╝  ╚═╝░░╚═╝╚═╝░░░░░╚═╝  ╚═════╝░╚═╝░░╚═╝╚══════╝╚═════╝░╚═╝░░╚═╝╚══════╝░╚═════╝░╚═════╝░

Random Integers -> file

0 likes • Nov 19, 2022 • 0 views
Java
//sample code to write 100 random ints to a file, 1 per line
import java.io.PrintStream;
import java.io.IOException;
import java.io.File;
import java.util.Random;
public class WriteToFile {
public static void main(String[] args) {
try {
PrintStream writer = new PrintStream(new File("randInts.txt"));
Random r = new Random();
final int LIMIT = 100;
for (int i = 0; i < LIMIT; i++) {
writer.println(r.nextInt());
}
writer.close();
} catch (IOException e) {
System.out.println("An error occured while trying to write to the file");
}
}
}

Word Counter

0 likes • Nov 19, 2022 • 0 views
Java
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class SimpleWordCounter {
public static void main(String[] args) {
try {
File f = new File("ciaFactBook2008.txt");
Scanner sc;
sc = new Scanner(f);
// sc.useDelimiter("[^a-zA-Z']+");
Map<String, Integer> wordCount = new TreeMap<String, Integer>();
while(sc.hasNext()) {
String word = sc.next();
if(!wordCount.containsKey(word))
wordCount.put(word, 1);
else
wordCount.put(word, wordCount.get(word) + 1);
}
// show results
for(String word : wordCount.keySet())
System.out.println(word + " " + wordCount.get(word));
System.out.println(wordCount.size());
}
catch(IOException e) {
System.out.println("Unable to read from file.");
}
}
}

try-catch modularity

0 likes • Jan 31, 2023 • 0 views
Java
public class UserService {
private UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User getUserById(long id) {
try {
return userRepository.getUserById(id);
} catch (Exception e) {
// Log the exception here
System.out.println("An error occurred while fetching the user: " + e.getMessage());
return null;
}
}
}
public class UserRepository {
public User getUserById(long id) throws Exception {
// Database query to fetch the user
if (id < 0) {
throw new Exception("Invalid user id");
}
// Return the user object
return new User(id, "John Doe");
}
}
public class User {
private long id;
private String name;
public User(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
}

Send Email from Java

0 likes • Nov 19, 2022 • 3 views
Java
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.example.com");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}