FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

update basics by realDuYuanChao · Pull Request #196 · examplehub/Java · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (9) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.examplehub.basics.number;

public class DoubleExample {
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.examplehub.basics.oop;

public class ObjectExample {
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public static boolean solution1(char[][] board) {
return false;
}
}

// travel all sub-box
// TODO
return true;
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,24 @@ void testTernaryOperator() {
int abs = num < 0 ? -num : num;
assertEquals(3, abs);
}

@Test
void testValueOf() {
Boolean b = Boolean.valueOf(true);
assertTrue(b.booleanValue());

b = Boolean.valueOf("false");
assertFalse(b.booleanValue());
}

@Test
void testAutoBoxing() {
Boolean b = false;
System.out.println(b);
assertFalse(b);

boolean bValue = b;
System.out.println(bValue);
assertFalse(bValue);
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,63 @@ void testAddAndSub() {
assertEquals(9, '9' - '0');
assertEquals(0, 'a' - 97);
}

@Test
void testValueOf() {
Character c = Character.valueOf('A');
assertEquals('A', c.charValue());
}

@Test
void testAutoboxing() {
Character c = 'A';
System.out.println(c);
assertEquals('A', c);
}

@Test
void testIsLower() {
assertFalse(Character.isLowerCase('A'));
assertTrue(Character.isLowerCase('a'));
assertFalse(Character.isLowerCase('1'));
}

@Test
void testIsUpper() {
assertTrue(Character.isUpperCase('A'));
assertFalse(Character.isUpperCase('a'));
assertFalse(Character.isUpperCase('1'));
}

@Test
void testIsDigit() {
System.out.println(Character.isDigit('A'));
System.out.println(Character.isDigit('a'));
System.out.println(Character.isDigit('1'));

assertFalse(Character.isDigit('A'));
assertFalse(Character.isDigit('a'));
assertTrue(Character.isDigit('1'));
}

@Test
void testToLower() {
assertEquals('a', Character.toLowerCase('A'));
}

@Test
void testToUpper() {
assertEquals('A', Character.toUpperCase('a'));
}

@Test
void testGetNumericValue() {
System.out.println(Character.getNumericValue('8'));
assertEquals(8, Character.getNumericValue('8'));

System.out.println(Character.getNumericValue('A'));
System.out.println(Character.getNumericValue('a'));
assertEquals(10, Character.getNumericValue('A'));
assertEquals(10, Character.getNumericValue('a'));
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.examplehub.basics.number;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class DoubleExampleTest {
@Test
void testValueOf() {
Double d = Double.valueOf(3.14);
System.out.println(d.doubleValue());
assertEquals(3.14, d.doubleValue());

d = Double.valueOf("3.14159");
System.out.println(d.doubleValue());
assertEquals(3.14159, d.doubleValue());
}

@Test
void testIsNaN() {
Double d = 0.0 / 0;
assertEquals(d, Double.NaN);
}

@Test
void testAutoboxing() {
Double d = 3.14;
assertEquals(3.14, d.doubleValue());
double pi = d;
assertEquals(3.14, pi);
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,51 @@

import static org.junit.jupiter.api.Assertions.*;

import jdk.jfr.StackTrace;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

class IntegerExampleTest {

@Test
void testValueOf() {
Integer integer = Integer.valueOf(100);
System.out.println(integer);
assertEquals(100, integer.intValue());

integer = Integer.valueOf("123");
System.out.println(integer);
assertEquals(123, integer.intValue());

integer = Integer.valueOf("a", 16);
System.out.println(integer);
assertEquals(10, integer.intValue());
}

@Test
void testParseInt() {
int number = Integer.parseInt("123");
System.out.println(number);
assertEquals(123, number);

assertThrows(NumberFormatException.class, () -> {
int num = Integer.parseInt("123a");
fail();
});
}

@Test
void testAutoBoxing() {
Integer integer = 10;
assertEquals(10, integer.intValue());
Integer integer1 = 10;
assertTrue(integer == integer1);
assertTrue(integer.equals(integer1));

Integer first = 128;
Integer second = 128;
System.out.println(first == second);
assertNotSame(first, second);
}

@Test
Expand All @@ -24,4 +59,19 @@ void testAutoBoxingNullPointer() {
assertTrue(true);
}
}

@Test
void testToBinaryString() {
assertEquals("1010", Integer.toBinaryString(10));
}

@Test
void testToOctalString() {
assertEquals("12", Integer.toOctalString(10));
}

@Test
void test(){
assertEquals("a", Integer.toHexString(10));
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.examplehub.basics.oop;

import org.junit.jupiter.api.Test;

import java.util.Objects;

import static org.junit.jupiter.api.Assertions.*;

class ObjectExampleTest {
@Test
void testObject() {
Object o = new Object();
System.out.println(o);
}

@Test
void testEquals() {
class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
}
class People {
private final String name;
private final int age;

public People(String name, int age) {
this.name = name;
this.age = age;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
People people = (People) o;
return age == people.age && Objects.equals(name, people.name);
}

@Override
public int hashCode() {
return 0;
}
}
Student stu1 = new Student(10001, "duyuanchao");
Student stu2 = new Student(10001, "duyuanchao");
assertNotSame(stu1, stu2);
assertNotEquals(stu1, stu2);

People p1 = new People("duyuanchao", 25);
People p2 = new People("duyuanchao", 25);
People p3 = new People("jack", 25);
assertNotSame(p1, p2);
assertEquals(p1, p2);
assertNotEquals(p1, p3);
}

@Test
void testHashCode() {
class People {
private final String name;
private final int age;

public People(String name, int age) {
this.name = name;
this.age = age;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
People people = (People) o;
return age == people.age && Objects.equals(name, people.name);
}

@Override
public int hashCode() {
return this.age % 13;
}
}
People p1 = new People("duyuanchao", 25);
People p2 = new People("duyuanchao", 25);
assertEquals(p1.hashCode(), p2.hashCode());
}

@Test
void testToString() {
class People {
private final String name;
private final int age;

public People(String name, int age) {
this.name = name;
this.age = age;
}

@Override
public String toString() {
return "name = " + name + ", age = " + age;
}
}

People people = new People("duyuanchao", 25);
assertEquals("name = duyuanchao, age = 25", people.toString());
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ void testRandom() {
double randomNumber = Math.random();
assertTrue(randomNumber >= 0 && randomNumber < 1);
}

@Test
void testRound() {
assertEquals(3, Math.round(3.14));
assertEquals(4, Math.round(3.78));
assertEquals(-3, Math.round(-3.14));
}
}

Back | FazBrowse Home | New Git URL