Skip to content

Instantly share code, notes, and snippets.

@mr-karan
Last active July 11, 2024 16:20
Show Gist options
  • Save mr-karan/42140edab46ee4f8314d to your computer and use it in GitHub Desktop.
Save mr-karan/42140edab46ee4f8314d to your computer and use it in GitHub Desktop.
Java Syntax cheatsheet
import java.util.Scanner;
import java.util.ArrayList;
public class hello {
public static void main(String[] args) throws Exception
{
Scanner reader = new Scanner(System.in);
String name =reader.nextLine();
System.out.println(name);
double a=45.23;
int ab=a.intValue(); //45
Scanner sc = new Scanner(System.in);
BigInteger a = sc.nextBigInteger();
BigInteger b = sc.nextBigInteger();
BigInteger sum=a.add(b);
BigInteger prod=a.multiply(b);
System.out.println(sum);
System.out.println(prod);
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
double y=sc.nextDouble();
sc.nextLine();
String s=sc.nextLine();
System.out.println("String: "+s);
System.out.println("Double: "+y);
System.out.println("Int: "+x);
int count=1;
TreeSet ts = new TreeSet();
for(int i=0;i<=word.length();i++)
{
for (int c=0;c<=word.length()-i;c++){
if(word.substring(i,c+i).length()==3){
ts.add(word.substring(i,c+i));
}
}
while(scanner.hasNext()){
String a=scanner.nextLine();
System.out.println(count+" "+a);
count++;
}
int number = Integer.parseInt(reader.nextLine());
while(number!=answer){
if(number>answer) {
System.out.println("Guessed Number is lesser" +" Guesses made = "+count);
}
else{
System.out.println("Guessed Number is greater" +" Guesses made = "+count);
}
number = Integer.parseInt(reader.nextLine());
count++;
}
ArrayList<String> wordList = new ArrayList<String>();
wordList.add("First");
wordList.add("Second");
Collections.sort(wordList);
for (String word : wordList) {
System.out.println( word );
}
ArrayList<Integer> numbers = new ArrayList<Integer>();
// JOptionsPane.showMessageDialog
numbers.add(4);
/*
next() v/s nextLine() :
next() returns till a complete token is given.
nextLine() advances the scanner till '\n' is present and moves the cursor down. next() keeps the cursor there only
*/
numbers.add(8);
// tries to remove the number from the index 4, does not work as expected!
numbers.remove(4);
// this removes the number 4 from the list
numbers.remove(Integer.valueOf(4));
}
public static int randNum(int min,int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
//OOPS in Java
public class Person{
int age;
int name;
public Person(String initnames){
this.name=initnames;
this.age=0;
}
public String toString(){
return this.name+" "+this.age;
}
public void setage(int agebyUser){
this.age=agebyUser;
}
public int getage(){
return this.age;
}
public boolean isAdult(){
if (this.age>18){
return true;
}
else{
return false;
}
}
//Object as argument to method
public boolean isAcceptedAsMember(Person person) {
if ( person.age() < this.age ) {
return false;
}
return true;
}
}
public class Man extends Person{
private String gender;
public Man(String name,int age) {
super(name, age);
this.gender = gender;
}
public String getMotorType() {
return motorType;
}
public String getGender(){
return gender;
}
@Override
public String toString() {
return super.toString() + "\n And my personal message again!";
}
}
//Customer - Order Inheritance example :
public class Customer{
private String name;
private String address;
public Customer(String name,String address){
this.name=name;
this.address=address;
}
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address=address;
}
}
public Order extends Customer{
private String product;
private Customer customer;
private String amount;
public Order(String product,String amount,String name,String)
{
super(name,address);
this.product=product;
this.amount=amount;
}
public Tilaus(Customer customer, String product, String amount) {
this.customer = customer;
this.product = product;
this.amount = amount;
}
public String getProduct(){
return product;
}
public String getAmount(){
return amount;
}
public String mailingaddress(){
return this.getName()+" "+this.getAddress();
}
public String mailingAddressSecondPart() {
return this.customer.getName() + "\n" + this.customer.getAddress();
}
}
}
//Socket Programming :
import java.net.*;
public class IdentifyHostNameIP {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
try {
InetAddress address = InetAddress.getByName(args[i]);
System.out.print("Host name: " + address.getHostName() + " ");
System.out.println("IP address: " + address.getHostAddress());
}
catch (UnknownHostException ex) {
System.err.println("Unknown host or IP address " + args[i]);
}
}
}
}
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class client {
private DataOutputStream toServer;
private DataInputStream fromServer;
public static void main(String[] args) {
new client();
}
public client()
{
Scanner reader = new Scanner(System.in);
double radius = reader.nextInt();
try {
Socket socket = new Socket("localhost", 8000);
fromServer = new DataInputStream(socket.getInputStream());
toServer = new DataOutputStream(socket.getOutputStream());
}
catch(IOException ex){
System.out.println("Unknown network issue");
}
try{
toServer.writeDouble(radius);
toServer.flush();
double area = fromServer.readDouble();
System.out.println("Area is : "+area);
}
catch(IOException ex){
System.err.println(ex);
}
}
}
// ^ client.java
// server.java (added gui for fun )
import java.awt.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import javax.swing.*;
public class server extends JFrame {
private JTextArea jta = new JTextArea();
public static void main(String[] args) {
new server();
}
public server()
{
//GUI
setLayout(new BorderLayout());
add(new JScrollPane(jta), BorderLayout.CENTER);
setTitle("server");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
//server Code
try {
ServerSocket serverSocket = new ServerSocket(8000);//Establish this server with the port 8000
jta.append("server started at " + new Date() + "\n");
//Allows server to listen for connections
Socket socket = serverSocket.accept();
InetAddress inetAddress = socket.getInetAddress();
System.out.println("Client's host name is " +
inetAddress.getHostName());
//Data streams for writing/reading data into the connected socket from above
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
while(true) {
double radius = inputFromClient.readDouble();
double area = radius * radius * Math.PI;
outputToClient.writeDouble(area);
jta.append("Radius recieved from client: " + radius + "\n");
jta.append("Area found: " + area + "\n");
}
}
catch(IOException ex){
System.err.println(ex);
}
}
}
Theory :
GUI :
import javax.swing.*;
public class MyFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("Test Frame");
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
}
// Add a button into the frame
frame.getContentPane().add(
new JButton("OK"));
JPanel p = new JPanel();
p.add(new JButton("OK"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment