面试java岗位,是需要考核java知识的,只有通过面试官的考核,才能胜任java岗位,所以除了需要打牢基础知识以外,还可以积累一些面试题,下面我们就给大家分享一些java面试场景设计题!
1. java 设计一个名为Complex的复数类来表示复数以及完成复数的运算,覆盖toString方法,实现Cloneable接口
import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Scanner; public class Complex implements Cloneable { //dataField private double realPart; private double imaginaryPart; //constructor public Complex() { realPart = 0; imaginaryPart = 0; } public Complex(double a) { realPart = a; } public Complex(double a, double b) { realPart = a; imaginaryPart = b; } //accessor public double getRealPart() { return realPart; } public double getImaginaryPart() { return imaginaryPart; } //method public Complex add(Complex a, Complex b) { Complex tempC = new Complex(a.realPart + b.realPart, a.imaginaryPart + b.imaginaryPart); return tempC; } public Object substract(Complex a, Complex b) { Complex tempC = new Complex(a.realPart - b.realPart, a.imaginaryPart - b.imaginaryPart); return tempC; } public Complex multiply(Complex a, Complex b) { Complex tempC = new Complex(a.realPart * b.realPart - a.imaginaryPart * b.imaginaryPart, a.realPart * b.imaginaryPart + a.imaginaryPart * b.realPart); return tempC; } public Complex divide(Complex a, Complex b) { double tempA = a.realPart * b.realPart + a.imaginaryPart * b.imaginaryPart; double tempB = a.imaginaryPart * b.realPart - a.realPart * b.imaginaryPart; double temp = Math.pow(b.imaginaryPart, 2) + Math.pow(b.realPart, 2); Complex tempC = new Complex(tempA / temp, tempB / temp); return tempC; } public double abs(Complex a) { return Math.sqrt(a.realPart * a.realPart + a.imaginaryPart * a.imaginaryPart); } @Override public String toString() { if (this.realPart == 0 && this.imaginaryPart != 0) return formatDouble(imaginaryPart) + "i"; else if (this.imaginaryPart > 0) return formatDouble(realPart) + "+" + formatDouble(imaginaryPart) + 'i'; else if (this.imaginaryPart < 0) return formatDouble(realPart) + "-" + formatDouble(imaginaryPart) + 'i'; else return "" + formatDouble(realPart); } @Override public Complex clone() { Complex tempC; try { tempC = (Complex) super.clone(); } catch (CloneNotSupportedException ex) { return null; } return tempC; } //美化浮点显示 public static String formatDouble(double d) { BigDecimal bg = new BigDecimal(d) .setScale(2, RoundingMode.UP); double num = bg.doubleValue(); if (Math.round(num) - num == 0) { return String.valueOf((long) num); } return String.valueOf(num); } public static void main(String[] args) { System.out.println("输入第一个复数的实部和虚部(空格隔开)"); Scanner input = new Scanner(System.in); double real_1 = input.nextDouble(); double imaginary_1 = input.nextDouble(); Complex c_1 = new Complex(real_1, imaginary_1); System.out.println("输入第二个复数的实部和虚部(空格隔开)"); //double real_2 = input.nextDouble(); //double imaginary_2 = input.nextDouble(); Complex c_2 = new Complex(input.nextDouble(), input.nextDouble()); System.out.println(c_1.toString() + " + " + c_2.toString() + " = " + c_1.add(c_1, c_2)); System.out.println(c_1.toString() + " - " + c_2.toString() + " = " + c_1.substract(c_1, c_2)); System.out.println(c_1.toString() + " * " + c_2.toString() + " = " + c_1.multiply(c_1, c_2)); System.out.println(c_1.toString() + " / " + c_2.toString() + " = " + c_1.divide(c_1, c_2)); } }
2.ThreadLocal是什么?底层如何实现的?写一个例子。
是一个解决线程并发问题的一个类,底层实现主要是存有一个map,以线程作为key,范型作为value。可以理解为线程级别的缓存。
使用起来比较简单 假设我们要实现一个线程级别的缓存。
private static final ThreadLocaltestInt = new ThreadLocal(); public static Map getMap() { Map s = (Map) testInt.get(); if (s == null) { s = new HashMap(); testInt.set(s); } return s; }
这样我们就能实现在调用这个方法的时候 获得的map是线程级别的。每一个线程都会获得一个单独的map。
3.给出一个非空的字符串,判断这个字符串是否是由它的一个子串进行多次首尾拼接构成的
给出一个非空的字符串,判断这个字符串是否是由它的一个子串进行多次首尾拼接构成的。输出最长的子串
例如,”abcabcabc”满足条件,因为它是由”abc”首尾拼接而成的,而”abcab”则不满足条件。
思想如下:从最长的二等分开始查找,用等分后的子字符串拼接成新的字符串B,与原字符串A进行比较,如果相等,返回这个字符串,如果不相等进行三等分以此类推,如果直至n等分(n=字符串A长度)都不能满足,输出false
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); System.out.println(binSearch(str)); } public static String binSearch(String str) { int len = 2; while (str.length() / len > 0) { if (str.length() % len != 0) { len++; continue; } else { StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { < p = "" > sb.append(str.substring(0, str.length() / len)); } if (sb.toString() .equals(str)) { return str.substring(0, str.length() / len); } len++; } } return "false"; } }
这些都是java面试场景设计题,而场景设计题在面试中的占分比重还是很大的,所以大家平时一定要多实践,提高技能!最后大家如果想要了解更多java面试题知识,敬请关注奇Q工具网。
推荐阅读: