|
Google for "Java image recognition".
|
|
|
|
|
i had starting learning java one month age but till now i dont think i had gain even 1% of knowledge please help
|
|
|
|
|
|
Hi,
I'm in a full stack training program and we are currently working on an inter-team project comprised of RESTful services in a microservice architecture. I've been put in charge of back-end unit testing for and am currently researching various test APIs. In addition to JUnit, I'm trying to determine whether I should use REST-assured, Mockito, or both. Reading through the documentation and reading/watching various tutorials has been a bit confusing though. My understanding so far is that REST Assured is for endpoint testing and Mockito is for unit testing. Can someone please give advice as to which would be better to use, or should I be using them both?
Basically, it would be for testing basic CRUD functionality in Spring MVC apps on the service and controller layers.
Any help or advice is greatly appreciated! Also, I can provide more information if you want, I realize it's kind of an open-ended question.
|
|
|
|
|
This is the Java forum. What language is that code in your picture?
|
|
|
|
|
It's written in "spam".
The words "tick to view" are a link to an essay writing site.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks, I (foolishly) assumed it was a single link.
|
|
|
|
|
Hey guys!
I'm just trying to figure out this homework and could use some help. Not looking for answers really, just a better understanding. I'm very very new to coding, using jGRASP java. The teacher told us to create this game that generates an output similar to this.
Round 1:
• Player A rolls: 1,2, the highest number is 21
• Player B rolls: 2,1, the highest number is 21
• Result: draw
• Round 2:
• Player A rolls: 2,5, the highest number is 52
• Player B rolls: 6,8 the highest number is 86
• Result: Player B wins
• Round 3:
• Player A rolls: 1,9, the highest number is 91
• Player B rolls: 9,9 the highest number is 99
• Result: Player B wins
• Final Result: Player B wins!!!
Do I have to declare the numbers as Strings to concatenate them? and do I use if else statements? Specifically, I'm confused on how to arrange the two numbers if one is greater than the other so the greater number will be placed in front.
import java.util.Scanner;
import java.util.Random;
public class beatThat
{
public static void main(String[] args)
{
Random r1 = new Random();
Random r2 = new Random();
Random r3 = new Random();
Random r4 = new Random();
int die1 = r1.nextInt(6) + 1;
int die2 = r2.nextInt(6) + 1;
int die3 = r3.nextInt(6) + 1;
int die4 = r4.nextInt(6) + 1;
System.out.println("ROUND 1");
System.out.println("Player A rolls: " + die1 + " and " + die2 +".");
System.out.println("Player B rolls: " + die3 + " and " + die4 +".");
if(die1 >= die2)
{
System.out.println("Player A's highest number is: " + die1 + die2);
}
else if (die2 >= die1)
{
System.out.println("Player A's highest number is: " + die2 + die1);
}
if(die3 >= die4)
{
System.out.println("Player B's highest number is: " + die3 + die4);
}
else if(die4 >= die3)
{
System.out.println("Player B's highest number is: " + die4 + die3);
}
}
}
Anything helps, thank you guys.
Have a good day!
modified 6-May-18 14:51pm.
|
|
|
|
|
Your call to nextInt returns a number in the range [0..5] so you will end up with a number in the range [1..6], so you can never roll an 8 or a 9, as specified in your examples.
To get the highest number you need to write a method which takes two numbers and returns the relevant value. The algorithm for this is fairly straightforward. Compare the two numbers to get the higher number (or either one if they are equal). Multiply the higher number by 10 and add the lower, which gives the "highest" value.
|
|
|
|
|
Hi. For the question below I wrote a code which you can see and it has an unknown error. I appreciate if someone can tell me where the problem is. Thanks.
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
class Solution {
public boolean isSymmetric(TreeNode root) {
boolean flag = false;
if (root == null) return true;
if(root.left == root.right != null)
flag = twoSymmetric(root.left , root.right);
retrun flag;
}
public boolean twoSymmetric(TreeNode left , TreeNode right){
if (left.left.val == right.right.val != null && left.right.val == right.left.val != null) return twoSymmetric(left.left , right.rigth) && twoSymmetric(left.right , right.left);
else return false;
}
}
|
|
|
|
|
It is not clear what your node structure is, but at a guess your code needs modifying to something like:
public boolean twoSymmetric(TreeNode left , TreeNode right){
if (left.val == right.val) {
if (left.left != null && right.right != null) {
if (twoSymmetric(left.left, right.right) {
if (left.right != null && right.left != null) {
return (twoSymmetric(left.right , right.left);
}
}
}
}
return false;
}
|
|
|
|
|
Text files like this:
1.txt :
cronie-anacron::1.4.11::17.el7::x86_64::Friday 05 January 2018 11:13:34 AM IST
2.txt :
accountsservice :: 0.6.40-2ubuntu11.3 :: None :: amd64 :: None
|
|
|
|
|
|
I have some DTOs that are written to be immutable.
private List<Transformation> foo;
... [omitted for brevity]
public List<Transformation> getFoo() {
List<Transformation> bar = new ArrayList<Transformation>(foo.size());
for (Transformation listEntry : foo) {
bar.add(Transformation.of(listEntry));
}
return bar;
}
This works for my current application as I happen to know that the Lists are all ArrayLists.
What if I didn't know that? Is there a way to deep copy the contents of an arbitrary implementation of List without knowing what type that underlying List is?
|
|
|
|
|
As long as ListType is clonable, you can do it as following
bar.add(listEntry.clone());
|
|
|
|
|
I can't seem to figure out what I am doing wrong with my while loop. For some reason it wont execute the loop it just goes on to the next piece of code. please let me know what is wrong with my syntax.
package com.nec;
import java.util.Scanner;
public class Inputs {
private static Scanner s;
private static String grade;
private static Boolean flag;
private static double averageSum;
private static int studentCounter;
private static Double classAverage;
public static void main(String[] args)
{
grade = "";
flag = true;
averageSum = 0;
studentCounter = 0;
While (flag); {
flag = (false);
s = new Scanner (System.in);
System.out.print("Enter your name: ");
String name = s.next();
System.out.print("Enter scores in three subjects: ");
int scores1 = s.nextInt();
int scores2 = s.nextInt();
int scores3 = s.nextInt();
double average = (scores1+scores2+scores3) /3.0;
averageSum = averageSum + average;
studentCounter++;
if (average >= 93) {
grade = "A";
}
if (average >= 90 && average < 93) {
grade = "A-";
}
if (average >= 87 && average < 90) {
grade = "B+";
}
if (average >= 83 && average < 87) {
grade = "B";
}
if (average >= 80 && average < 83) {
grade = "B-";
}
if (average >= 77 && average < 80) {
grade = "C+";
}
if (average >= 73 && average < 77) {
grade = "C";
}
if (average >= 70 && average < 73) {
grade = "C-";
}
if (average >= 67 && average < 70) {
grade = "D+";
}
if (average >= 63 && average < 67) {
grade = "D";
}
if (average >= 60 && average < 63) {
grade = "D-";
}
if (average >= 0 && average < 60) {
grade = "F";
}
System.out.println("\nName: " +name);
System.out.println("Average: " +average);
System.out.println("grade:" +grade);
System.out.println("Do you have another set of scores to compute, enter 1 for yes and 0 for no");
int ans2 = s.nextInt();
if (ans2 == 1) flag = true;
if (ans2 == 0) flag = false;}
classAverage = averageSum / studentCounter;
System.out.println("Class Average is:" +classAverage);
}
private static void While(Boolean flag2) {
}
}
|
|
|
|
|
While (flag); {
flag = (false);
You have a semi-colon after the expression which means that is the end of the while statement.
private static void While(Boolean flag2) {
}
Using While for a method name is bound to cause confusion and possible problems. Do not use names that could be mistaken. And yes, I know it is spelled slightly differently.
|
|
|
|
|
Hi all! I build a JUnit BookTestCase to test the minPrice() method in the BookApp class. The method is to find and return the minimum price of an array of book objects. But the test fails, and it shows "AssertionFailedError: expected: <34.99> but was: <0.0>". I don't know where the problem is, and where the 0.0 comes from. Thanks in advance!
The BookTestCase:
class BookTestCase {
private BookApp obj;
private Book[] book = new Book[5];
@BeforeEach
void setUp() throws Exception {
this.obj = new BookApp();
book[0] = new Book(1, "HTLM", 56.0);
book[1] = new Book(2, "Java", 128.0);
book[2] = new Book(3, "Python", 34.99);
book[3] = new Book(4, "C#", 40.0);
book[4] = new Book(5, "C", 115.5);
}
@Test
void testPopulateBooks() {
Assertions.assertThrows(IllegalArgumentException.class, () -> obj.populateBooks(6));
}
@Test
void testMinPrice() {
assertEquals(34.99, BookApp.minPrice(book), 0.0001);
}
}
The minPrice() in BookApp class:
public class BookApp {
public static double minPrice(Book[] b) {
double min = b[0].getPrice();
for (int i = 1; i < b.length; i++) {
if (b[i].getPrice() < min)
min = b[i].getPrice();
}
return min;
}
}
|
|
|
|
|
The only way to find the answer is to step through the code in the debugger. You may also want to check what obj.populateBooks(6) does.
|
|
|
|
|
Your setup() method is not doing what you think it is. Or I guess another way to look at that would be that you are not passing to assertEquals what you think you are.
|
|
|
|
|
I spent at least 10 more minutes staring at that code, and still don't see it.
modified 22-Apr-18 8:39am.
|
|
|
|
|
Hello.
I am trying to prepare a window program GUI to display invoices from the system add a new invoice (and add invoice items as part of the new invoice), modify the unclosed invoice and view the details of the invoice.
The program should be written in Swing without the use of window editors. (only code). I have problem with buttons to adds new invoice with some positions.
my concept
[img]https://i.imgur.com/Ph7vndL.png[/img]
classes:
public class TestInvoice
package zda;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpringLayout;
public class TestInvoice extends JFrame
{
public static void main(String[] args)
{
Contractor k1 = new Contractor("Some company \nsome adress, adress", "123-123-123");
Invoice f1 = new Invoice(k1);
Invoice f2 = new Invoice(k1);
f1.addPosition("Kredki 3szt", Invoice.Measure.QTY, 5, 3.2, Invoice.VAT.s23);
f1.addPosition("Flamastry 6szt", Invoice.Measure.QTY, 5, 4.59, Invoice.VAT.s23);
f1.addPosition("Plastelina 12 kolorów", Invoice.Measure.QTY, 2, 8.22, Invoice.VAT.s23);
f1.addPosition("Ołówki 3szt", Invoice.Measure.QTY, 1, 6.00, Invoice.VAT.s23);
f1.addPosition("Ołówkek HB", Invoice.Measure.QTY, 5, 1.2, Invoice.VAT.s08);
System.out.println(f1);
f2.addPosition("Kredki 3szt", Invoice.Measure.QTY, 5, 3.2, Invoice.VAT.s23);
f2.addPosition("Flamastry 6szt", Invoice.Measure.QTY, 5, 4.59, Invoice.VAT.s23);
f2.addPosition("Plastelina 12 kolorów", Invoice.Measure.QTY, 2, 8.22, Invoice.VAT.s23);
f2.close();
f2.addPosition("Ołówki 3szt", Invoice.Measure.QTY, 1, 6.00, Invoice.VAT.s23);
f2.addPosition("Ołówkek HB", Invoice.Measure.QTY, 5, 1.2, Invoice.VAT.s08);
System.out.println(f2);
System.out.println(Math.round(2022.0000000000002) / 100.0);
new TestInvoice();
}
private InvoiceManager manager;
public TestInvoice()
{
manager = new InvoiceManager(5);
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
JPanel leftPanel = new JPanel();
leftPanel.setSize(400, 500);
GroupLayout gl = new GroupLayout(leftPanel);
gl.setAutoCreateGaps(true);
gl.setAutoCreateContainerGaps(true);
leftPanel.setLayout(gl);
SpringLayout sl = new SpringLayout();
JPanel rightPanel = new JPanel(sl);
rightPanel.setSize(400, 500);
JSplitPane sp = new JSplitPane();
sp.setRightComponent(rightPanel);
sp.setLeftComponent(leftPanel);
sp.setPreferredSize(new Dimension(600, 600));
JLabel lPositionsName = new JLabel("name");
JLabel lPositionsMeasure = new JLabel("Measure");
JLabel lPositionsQty = new JLabel("qty");
JLabel lPositionsPrice = new JLabel("price");
JLabel lPositionsTax = new JLabel("podatek");
JTextField tfName = new JTextField("", 20);
JFormattedTextField tfMeasure = new JFormattedTextField(NumberFormat.getNumberInstance());
JFormattedTextField tfQty = new JFormattedTextField(NumberFormat.getNumberInstance());
JFormattedTextField tfPrice = new JFormattedTextField(NumberFormat.getNumberInstance());
JFormattedTextField tfTax = new JFormattedTextField(NumberFormat.getNumberInstance());
gl.setVerticalGroup(gl.createSequentialGroup()
.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsName)
.addComponent(tfName))
.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsMeasure)
.addComponent(tfMeasure))
.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsQty)
.addComponent(tfQty))
.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsPrice)
.addComponent(tfPrice))
.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsTax)
.addComponent(tfTax)));
gl.setHorizontalGroup(gl.createSequentialGroup()
.addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(lPositionsName)
.addComponent(lPositionsMeasure).addComponent(lPositionsQty).addComponent(lPositionsPrice)
.addComponent(lPositionsTax))
.addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(tfName)
.addComponent(tfMeasure).addComponent(tfQty).addComponent(tfPrice).addComponent(tfTax)));
JList<Invoice> list = new JList<Invoice>();
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(250, 80));
rightPanel.add(listScroller);
JLabel error = new JLabel("");
error.setForeground(Color.red);
JButton b = new JButton("save");
b.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
String name = tfName.getText();
int Measure = ((Number) tfMeasure.getValue()).intValue();
int qty = ((Number) tfQty.getValue()).intValue();
int price = ((Number) tfPrice.getValue()).intValue();
int tax = ((Number) tfTax.getValue()).intValue();
}
});
JButton o = new JButton("read");
o.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
list.setListData(manager.getList());
}
});
JButton u = new JButton("delete");
u.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
List<Invoice> listt = list.getSelectedValuesList();
for (Invoice w : listt)
{
manager.removeInvoice(w);
}
list.setListData(manager.getList());
}
});
getContentPane().add(sp);
getContentPane().add(error);
getContentPane().add(b);
getContentPane().add(o);
getContentPane().add(u);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Invoice
package zda;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
public class Invoice
{
private Contractor client;
private LocalDate dateOfIssue;
private String nr;
private static class Identifier
{
static int currentNumber = 0;
static int currentMonth = 0;
static int currentYear = 0;
static String generateNr()
{
LocalDate ld = LocalDate.now();
int year = ld.getYear();
int month = ld.getMonthValue();
if (year > currentYear)
{
currentYear = year;
currentMonth = ld.getMonthValue();
currentNumber = 0;
} else if (month > currentMonth)
{
currentMonth = month;
currentNumber = 0;
}
return (++currentNumber) + "/" + currentMonth + "/" + currentYear;
}
}
public enum Measure
{
QTY, M, L, KG, M2
}
public enum VAT
{
s23(.23), s08(.08), s05(.05), s00(.0);
public double rate;
public String str;
VAT(double rate)
{
this.rate = rate;
this.str = String.format("%.0f%%", 100 * rate);
}
}
private class Position
{
private int position;
private String name;
private Measure mmasure;
private double quantity;
private double unitPriceWithoutTax;
private VAT tax;
public Position(String name, Measure mmasure, double quantity, double price, VAT tax)
{
this.name = name;
this.mmasure = mmasure;
this.quantity = quantity;
this.unitPriceWithoutTax = price;
this.tax = tax;
this.position = positions.size() + 1;
}
public double getValue()
{
return quantity * unitPriceWithoutTax;
}
public double getTax()
{
double d = quantity * unitPriceWithoutTax * tax.rate;
d = Math.round(d * 100) / 100.;
return d;
}
@Override
public String toString()
{
return String.format("%5d | %30s | %10s | %10s | %10s | %10s | %10.2f", position, name, mmasure, quantity,
unitPriceWithoutTax, tax.str, getValue() + getTax());
}
}
public static String heading()
{
return String.format("%5s | %30s | %10s | %10s | %10s | %10s | %10s", "LP.", "name", "mmasure", "quantity",
"Unit price.", "tax", "price with tax");
}
private ArrayList<Position> positions = new ArrayList<Position>();
public void addPosition(String name, Measure m, double quantity, double price, VAT tax)
{
if (closed)
return;
Position p = new Position(name, m, quantity, price, tax);
positions.add(p);
double d = subtotals.get(tax);
d += p.getTax();
subtotals.put(tax, d);
sumWithTax += p.getValue();
sumPayment += p.getValue() + p.getTax();
}
private boolean closed = false;
public void close()
{
closed = true;
}
private HashMap<VAT, Double> subtotals = new HashMap<>();
private double sumWithTax = 0;
private double sumPayment = 0;
public Invoice(Contractor c)
{
nr = Identifier.generateNr();
client = c;
dateOfIssue = LocalDate.now();
for (VAT v : VAT.values())
{
subtotals.put(v, 0.);
}
}
@Override
public String toString()
{
String invoice = "---------------------------------------------\n";
invoice += (closed ? "C-" : "O-") + "invoice no." + nr + "\n";
invoice += "from date " + dateOfIssue.format(DateTimeFormatter.ofPattern("d-M-y")) + "\n";
invoice += "\nFor: \n" + client + "\n\n";
invoice += heading() + "\n";
for (Position p : positions)
{
invoice += p.toString() + "\n";
}
invoice += "\n";
invoice += String.format("%-15s: %10.2f\n", "Sum without tax", sumWithTax);
for (HashMap.Entry<VAT, Double> e : subtotals.entrySet())
{
if (e.getValue() > 0)
invoice += String.format("%-15s: %10.2f\n", "SUM " + e.getKey().str, e.getValue());
}
invoice += String.format("%27s\n", "+ ----------");
invoice += String.format("%-15s: %10.2f\n", "sum to pay", sumPayment);
invoice += "---------------------------------------------\n";
return invoice;
}
}
public class InvoiceManager
package zda;
import java.util.ArrayList;
public class InvoiceManager
{
private int maxNumber = 10;
private ArrayList<Invoice> listOfInvoices = new ArrayList<Invoice>();
public void addInvoice(Invoice w) throws IllegalArgumentException{
if(listOfInvoices.size()< maxNumber)
listOfInvoices.add(w);
else
throw new IllegalArgumentException("No space for a new invoice");
}
public void removeInvoice(Invoice w){
listOfInvoices.remove(w);
}
public Invoice[] getList(){
return (Invoice[]) listOfInvoices.toArray(new Invoice[listOfInvoices.size()]);
}
public InvoiceManager(int max){
maxNumber = max;
}
}
public class Contractor
package zda;
public class Contractor
{
String data;
String id;
public Contractor(String data, String id)
{
this.data = data;
this.id = id;
}
@Override
public String toString()
{
return data + "\n" + "NIP: " + id;
}
}
|
|
|
|
|
|
I have a program that displays invoices f1 and f2 of the client in the console and I want to extend it with a GUI graphic layout, which will allow adding a new invoice f3 (f4, f5 ...) along with the items using the button.
And I do not know how to do it.
I can see the GUI as follows:
[img]https://i.imgur.com/Jk0GIdl.png[/img]
These are not compiler errors, I just do not know how to write a code to get in gui: adding invoices, adding items to the invoice, editing invoice notifications and presenting the entire invoice.
I think about it for a few days and I do not know how to write it.
|
|
|
|
|
|