\(sin^2(\theta) + cos^2(\theta) =1 \)
0 notes
Simple Inheritance example
This is one of the simplest inheritance example. I used super keyword to inherit Chocolate’s constructor and to display divideIt method (of each class). Keep reading for the code.
class Chocolate {
private String color;
private int size;
public Chocolate(String color, int size) {
this.color = color;
this.size = size;
System.out.println("Color and size of choco are: "+ color + ", " + size);
}
public void divideIt() {
System.out.println("Choco has been inherited.");
}
}
class Nutella extends Chocolate {
public Nutella(String color, int size) {
super(color, size);
System.out.println("Color and size of nuttie are: "+ color + ", " + size);
}
public void divideIt() {
System.out.println("Ferrero has appeared.");
}
public void trick() {
Nutella rocher = new Nutella("bitter", 16);
rocher.divideIt();
super.divideIt();
}
}
public class Examplor {
public static void main(String[] args) {
Chocolate yummy = new Chocolate("white", 32);
Nutella delicious = new Nutella("black", 240);
delicious.trick();
}
}
1 note
·
View note
Simple example of Constructor usage - part II, with Strings
After a longer while spent on this lil code I have finally figured it out.
Code’s destination is to read two user’s inputs - String and int, then program introduces us a constructor with those values. Basically it’s the same code from the previous entry but modified with a String array as a method parameter.
Actually, the biggest problem was to pass proper Strings into void speech. My mistake was following: I did an ugly, superfluous loop which changed wordlist[i] into speech.words. It was unnecessary, obviously - all I had to do was to write object.method(main-class_array_name) :) Here’s the code.
import java.util.Scanner;
import java.lang.String;
class Classy {
public static int count = 0;
private int value;
private String name;
public String[] words = new String[3];
public Classy(int value, String name) {
this.value = value;
this.name = name;
count++;
System.out.print(value + ", " + name + ", " + count);
}
public void speech (String[] words) {
StringBuilder sb = new StringBuilder();
sb.append(words[0]).append(words[1]).append(words[2]);
System.out.println(sb.toString());
}
}
public class Main {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter value then name");
int inputValue = input.nextInt();
input.nextLine();
String inputName = input.nextLine();
Classy testosteron = new Classy(inputValue, inputName);
String[] wordlist = new String[3];
System.out.println("Enter 3 phrases now");
for (int i=0; i<3; i++) {
wordlist[i] = input.nextLine();
}
testosteron.speech(wordlist);
}
}
0 notes
Simple example of Constructor usage - part I
Here’s a quick example of a declared constructor with two private arguments, using this. . I added some kind of counter using static variable - it counts number of times a constructor has been used. Ignore code in the comment.
import java.util.Scanner;
class Classy {
public static int count = 0;
private int value;
private String name;
//String[] words = new String[3];
public Classy(int value, String name) {
this.value = value;
this.name = name;
count++;
System.out.print(value + name + count);
}
/* public void speech (String[] words) {
StringBuilder sb = new StringBuilder();
sb.append(words[0]).append(words[1]).append(words[2]);
System.out.println(sb.toString());
} */
}
public class Main {
public static void main (String[] args) {
//Scanner input = new Scanner(System.in);
Classy testie = new Classy(16, "Bob");
Classy testie2 = new Classy(18, "John");
}
}
0 notes
Simple Calculator in Java
I made a really simple calculator in Java to ‘practice’ a bit.
Following functions are contained in: addition (double), subtraction (double), multiplication (double), division (double), modulo division (int). Everything is in loop thanks to while(bufor==0). Code’s below.
import java.util.Scanner; class Calculator {
double number1;
double number2; double getAddition() {
return number1+number2;
} double getSubtraction() {
return number1-number2;
} double getMulti() {
return number1*number2;
} double getDivision() {
return number1/number2;
} int getModulo() {
return ((int)number1)%((int)number2);
}
}public class MainClass {
public static void main(String[] args) {
Calculator calculate = new Calculator();
Scanner input = new Scanner(System.in);
int option;
int bufor=0; while(bufor==0) {
System.out.println("Enter number1 then number2 and choose an option:");
System.out.println("1] Add 2] Subtract 3] Multiply 4] Divide 5] Modulo");
calculate.number1 = input.nextDouble();
input.nextLine();
calculate.number2 = input.nextDouble();
input.nextLine(); option = input.nextInt();
input.nextLine(); switch (option) {
case 1:
System.out.println(calculate.getAddition());
break;
case 2:
System.out.println(calculate.getSubtraction());
break;
case 3:
System.out.println(calculate.getMulti());
break;
case 4:
System.out.println(calculate.getDivision());
break;
case 5:
System.out.println(calculate.getModulo());
break;
default:
System.out.println("error");
break;
} System.out.println("Repeat? 1 NO 0 YES");
bufor = input.nextInt();
input.nextLine();
}
}
}
0 notes
Advanced Hello World in Java
A rudimentary example of Java syntax.
import java.util.Scanner;
class Mankind {
String name;
int age;
String getName() {
return name;
}
int getAge() {
return age;
}
}
public class MainClass {
public static void main(String[] args) {
System.out.println("Hello human, it's a stupid program.");
Mankind man = new Mankind();
man.name = "Paul";
man.age = 19;
System.out.println("How many loops?");
Scanner input = new Scanner(System.in);
int loops = input.nextInt();
for (int i=0; i<loops; i++) {
System.out.println(man.getAge()+", "+man.getName());
}
}
}
0 notes
First entry
Welcome to my blog, name is Paul.
I am here to save all things I can gather, mostly about mathematics and programming. Nevertheless, I will do my best to change those entries sometimes.
Feel free to ask me a question or suggest a problem.
0 notes