I am certainly facing a problem with a task i have at university:
public class Num<Integer> {
Integer x;
Integer y;
public Num(Integer x, Integer y) {
this.x = x;
this.y = y;
}
// now i have to write a method whitch returns (x, y) as a String
public String intValue() {
}
I have no idea if i have the correct basic approach or how this will work.
I wanted to write in the method intValue:
int i = x.IntValue();
int j = y.IntValue();
return "(" i "," j ")";
EDIT: I solved the problem. May someone confirm whether it is correct:
public class Zahl<T> {
public Integer x;
public Integer y;
public Zahl(Integer x, Integer y) {
this.x = x;
this.y = y;
}
public String intValue() {
int i = this.x.intValue();
int j = this.y.intValue();
return "(" + i + "," + j + ")";
}
}
EDIT 2: Problem solved! Thanks for helping out that fast :-)
Here is the correct code:
class Zahl<N extends Number> {
N x;
N y;
Zahl(N x, N y) {
this.x = x;
this.y = y;
}
String intValue() {
return "(" + x.intValue() + "," + y.intValue() + ")";
}
public class Test {
public static void main(String[] args) {
Zahl<Integer> z = new Zahl<>(7, 8);
System.out.println(z.intValue());
}
}
Output: (7,8)
Using Integer for the name of the type parameter is a very bad idea... Call it N or T but not Integer...
public class Num<N> {
...
}
Now the only thing we know about N is that it's an Object, and Object doesn't have an intValue method. So you probably want to make sure that N is a Number:
public class Num<N extends Number> {
...
}
Now you can write your class:
public class Num<N extends Number> {
N x;
N y;
public Num(N x, N y) {
this.x = x;
this.y = y;
}
public String intValue() {
return "(" + x.intValue() + ", " + y.intValue() + ")";
}
}
You can concat the fields and return that e.g.
public String intValue() {
return (x + " " + y);
}
Related
I want to calculate the coordinates of the points of the triangle when rotated by 45 degrees.
I decided to use this formula.but I don't understand where I made a mistake, since I kind of wrote everything correctly.
While for the x coordinates I get the correct results, but the problem is with y.
can you tell me what I was wrong about?thank you in advance.
class Point {
Double x;
Double y;
public Point(double x, double y) {
this.setX(x);
this.setY(y);
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public Point() {
x=0.0;
y=0.0;
}
public String toString() {
return x + " " + y + "\n";
}
}
class Triangle{
public Point A;
public Point B;
public Point C;
public Triangle (Point _a, Point _b,Point _c) {
A=_a;
B=_b;
C=_c;
public void sd_alfa(double d) {
Point CG=new Point((A.x+B.x+C.x)/3.0,(A.y+B.y+C.y)/3.0);
A.x=CG.x+(A.x-CG.x)*Math.cos(d)-(A.y-CG.y)*Math.sin(d);
A.y=CG.y+(A.x-CG.x)*Math.sin(d)+(A.y-CG.y)*Math.cos(d);
B.x=CG.x+(B.x-CG.x)*Math.cos(d)-(B.y-CG.y)*Math.sin(d);
B.y=CG.y+(B.x-CG.x)*Math.sin(d)+(B.y-CG.y)*Math.cos(d);
C.x=CG.x+(C.x-CG.x)*Math.cos(d)-(C.y-CG.y)*Math.sin(d);
C.y=CG.y+(C.x-CG.x)*Math.sin(d)+(C.y-CG.y)*Math.cos(d);
}
public String Draw() {
return " "+"A "+ A + " "+ "B "+ B + " "+ "C "+ C;
}
}
public class EightLab {
public static void main(String[] args) {
// TODO Auto-generated method stub
Point a = new Point(1,1);
Point b = new Point(3,3);
Point c = new Point(3,1);
System.out.println("A = " + a.toString());
Triangle T = new Triangle(a, b, c);
System.out.println("Triangle = \n" + T.Draw());
//T.sd_up(2.0);
// System.out.println("Triangle = \n" + T.Draw());
T.sd_alfa(Math.toRadians(45.0));
System.out.println("Triangle = \n" + T.Draw());
}
}
My result:
A 1.8619288125423017 0.8619288125423017
B 1.8619288125423021 2.276142374915397
C 3.276142374915397 1.8619288125423017
The result I want to get:
1.8633095244169 0.25578643762691
1.8633095244169 3.0842135623731
3.27752308679 1.67
package polygongeneric;
import java.util.ArrayList;
public class Polygon {
private ArrayList <Point2d> p = null;
private int points = 0;
public Polygon() { }
public Polygon(int numPoints) {
p = new ArrayList<>();
}
public boolean addPoint(Point2d point) {
p.add(points, point);
points++;
return true;
}
public boolean addPoint(double x, double y) {
Point2d a = new Point2d(x,y);
p.add(points, a);
return true;
}
#Override
public String toString() {
String s = "";
for (int i=0; i<points; i++)
s += p.get(i).toString() + "\n";
return s;
}
}
I'm trying to convert a class from using an array of references to Point2d objects as type Point2d. This is what I have so far but it's not outputting the answer that it's supposed to.
This is what my code outputs
(0.1,0.9)
(0.5,0.5)
(0.2,0.5)
This is what it's supposed to output
(0.1,0.9)
(0.3,0.7)
(0.5,0.5)
(0.4,0.8)
(0.2,0.5)
Do you guys have any idea. What I'm doing wrong?
This is my Point2d class
package polygongeneric;
public class Point2d {
private double x = 0, y = 0;
public Point2d() { }
public Point2d(double x, double y) {
setX(x);
setY(y);
}
public void setX(double initX) {
if (initX >= 0 && initX <= 1)
x = initX;
}
public void setY(double y) {
if (y >= 0 && y <= 1)
this.y = y;
}
public double getX() { return x; }
public double getY() { return y; }
public String toString() {
return "(" + x + "," + y + ")";
}
}
This is my main method
package polygongeneric;
public class PolygonGeneric {
public static void main(String[] args) {
Polygon p = new Polygon(5);
p.addPoint(new Point2d(.1, .9));
p.addPoint(.3, .7);
p.addPoint(new Point2d(.5, .5));
p.addPoint(.4, .8);
p.addPoint(new Point2d(.2, .5));
System.out.println(p);
}
}
You are not incrementing the position in your addPoint(double x, double y), so basically, you are replacing the existing point with a new point, so you are missing few point values and you need to correct the correct the code as shown below:
public boolean addPoint(double x, double y) {
Point2d a = new Point2d(x, y);
p.add(points, a);
points++;
return true;
}
Because you are simply adding the point at the end of the list, I suggest you can directly use arraylist.add(point); so that you will not get into these increment/other issues.
Also, you can change your constructor of Polygon class (which accepts int) as follows because you are not using the numPoints variable or else use an array with numPoints as the size instead of ArrayList.
public Polygon() {
p = new ArrayList<>();
}
You did not increment points in the addPoint(double x, double y) function.
Why not reuse the same method? and call the overloaded function
public boolean addPoint(Point2d point); instead of writing the same logic again and again.
public boolean addPoint(double x, double y) {
Point2d a = new Point2d(x,y);
return addPoint(a);
}
I am trying to print the array that I have created in the Lab12 class using nested for loop and the getX() and getY() methods that I created in the MyPoint class. However my loop is just giving me the reference. how do i get the values to print
pt[0][0] = (0.5, 1.2)
pt[0][1] = (0.0, 3.14)
pt[0][2] = (15.0, 27.5)
pt[1][0] = (6.6, 7.7)
pt[1][1] = (1.2, 2.1)
pt[1][2] = (12.0, 127.0)
public class MyPoint {
private double x;
private double y;
public MyPoint(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
public class Lab12 {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyPoint [][]pointMatrix = {
{new MyPoint(0.5,1.2), new MyPoint(0.0,3.14), new MyPoint(15.0,27.5)},
{new MyPoint(6.6,7.7), new MyPoint(1.2,2.1), new MyPoint(12.0,127.0)}
};
for (int i=0; i<pointMatrix.length; i++) {
for (int j=0; j<pointMatrix[i].length; j++){
System.out.print(pointMatrix[i][j] + " ");
}
System.out.println();
}
}
The best option, as #Lashane mentioned, is to implement the toString() method:
public class MyPoint {
// ...
#Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
Inside your loop:
System.out.println("pt[" + i + "][" + j + "] = " + pointMatrix[i][j]);
Ok, so I have made a toString() method for this Coordinate Class, but when I try to print a Coordinate using system.out.print(), it seems to ignore my method and just use the Object.toString() method, and just returns a memory address.
Here is my code for the toString method:
package spacetable;
public class Coordinate {
private int x;
private int y;
public Coordinate(){
x=0;
y=0;
}
public Coordinate(int x, int y){
this.x = x;
this.y = y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public double distTo(Coordinate xy){
double run = xy.getX() - this.getX();
double rise = xy.getY() - this.getX();
double dist = sqrt(run*run + rise*rise);
return dist;
}
public double distTo(int x, int y){
double run = x - this.getX();
double rise = y - this.getX();
double dist = sqrt(run*run + rise*rise);
return dist;
}
#Override
public String toString(){
String Strx = Integer.toString(x);
String Stry = Integer.toString(y);
String result = "(" Strx + ", " + Stry + ")";
return result;
}
}
and my code that tries to print:
package spacetable;
public class CordinateTest {
public static void main(String[] args) {
Coordinate place = new Coordinate(2,3);
System.out.println(place);
}
}
And the output is:
spacetable.Coordinate#e53108
why is my toString() being ignored?
Your code works fine for me, take a look here
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static class Coordinate {
private int x = 3;
private int y = 5;
#Override
public String toString(){
String Strx = Integer.toString(x);
String Stry = Integer.toString(y);
String result = "(" + Strx + ", " + Stry + ")";
return result;
}
}
public static void main (String[] args) throws java.lang.Exception
{
Coordinate place = new Coordinate();
System.out.println(place);
}
}
are you sure you recompiled after you added the toString method. If you are using an IDE, please check that build automatically is set. If not build the code again
Also, can you paste the import command. just want to make sure you are importing your own Coordinate class and not a class of some third party jar
public class Coordinate {
private int x;
private int y;
public Coordinate(int x,int y)
{
this.x=x;
this.y=y;
}
public String toString()
{
String result = "("+ x + ", " + y + ")";
return result;
}
public static void main(String[] args) {
Coordinate place = new Coordinate(2,3);
System.out.println(place);
}
}
Hi im new to this site and need help with a program im working on. the problem im having is that i cant seem to store string and two integers (as the coordinates). i have looked at other code but dont see how the values are stored. below is the code ive been using. the code seems to be fine but when trying to stored the values i cant put multiply integers. thanks for your time
import java.util.HashMap;
public class map {
class Coords {
int x;
int y;
public boolean equals(Object o) {
Coords c = (Coords) o;
return c.x == x && c.y == y;
}
public Coords(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int hashCode() {
return new Integer(x + "0" + y);
}
}
public static void main(String args[]) {
HashMap<Coords, Character> map = new HashMap<Coords, Character>();
map.put(new coords(65, 72), "Dan");
}
}
There is a class in java called Class Point.
http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html
This is the same information provided on Java docs API 10:
https://docs.oracle.com/javase/10/docs/api/java/awt/Point.html
A point representing a location in (x,y) coordinate space, specified in integer precision.
You can see an example, and also other important topics related in this link: http://www.java2s.com/Tutorial/Java/0261__2D-Graphics/Pointclass.htm
import java.awt.Point;
class PointSetter {
public static void main(String[] arguments) {
Point location = new Point(4, 13);
System.out.println("Starting location:");
System.out.println("X equals " + location.x);
System.out.println("Y equals " + location.y);
System.out.println("\nMoving to (7, 6)");
location.x = 7;
location.y = 6;
System.out.println("\nEnding location:");
System.out.println("X equals " + location.x);
System.out.println("Y equals " + location.y);
}
}
I hope this can help you!
There seems to be several issues:
"Dan" is a String, not a Character
case is important in Java (new coords(65,72) should be new Coords(65,72))
Coords needs to be static to be instantiated without a reference to an instance the enclosing map class.
This should work:
static class Coords {
...
}
Map<Coords, String> map = new HashMap<Coords, String>();
map.put(new Coords(65, 72), "Dan");
ps: although you are allowed to name a local variable map within a class map, it is not a good idea to have such name collision. In Java, classes generally start in upper case, so you could rename your class Map. But it happens that Map is a standard class in Java. So call your class Main or Test or whatever is relevant. ;-)
Adding to #assylias
Make you inner class static in order to insert new objects like you have or new Outer().new Inner() .
Take care of Java Naming Convention
Code like:
public class XYTest {
static class Coords {
int x;
int y;
public boolean equals(Object o) {
Coords c = (Coords) o;
return c.x == x && c.y == y;
}
public Coords(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int hashCode() {
return new Integer(x + "0" + y);
}
}
public static void main(String args[]) {
HashMap<Coords, String> map = new HashMap<Coords, String>();
map.put(new Coords(65, 72), "Dan");
map.put(new Coords(68, 78), "Amn");
map.put(new Coords(675, 89), "Ann");
System.out.println(map.size());
}
}
package Lecture3;
import java.util.Scanner;
public class lecture9 {
private int nInleste;
public lecture9() {/*
* tabell/ // T/*chapter 6 in the books.
**/
}
public static void main(String[] args) {
Scanner inn = new Scanner(System.in);
int nInleste = 3;
double[] tall = new double[nInleste];
double sum = 0;
for (int i = 0; i < nInleste; i++) {
System.out.println("Leste en tall!");
tall[i] = inn.nextDouble();
sum += tall[i];
}
System.out.println(sum);
double snitt = nInleste / nInleste;
System.out.println("Gjennomsnittsverdien:" + snitt);
for (int i = 0; i < nInleste; i++) {
double aavik = tall[i] - snitt;
int avvivk = 0;
System.out.println(i + 1 + " Tal sitt avvik fra gjennomsnittet " + avvivk);
}
}/* end of the main methods */
}
if you have problem with your code you can try this , simple code to store string and two int values into a map
class MyCoord{
private int X;
private int Y;
public MyCoord() {
this(0,0);
}
public MyCoord(int X, int Y) {
this.X = X;
this.Y = Y;
}
public int getX() {
return X;
}
public int getY() {
return Y;
}
public void setX(int X) {
this.X = X;
}
public void setY(int Y) {
this.Y = Y;
}
}
//main class begins
public class PointDemo{
public static void main(String[] args) {
Map <String,MyCoord> multiplePoints=new HashMap<String, MyCoord>();
multiplePoints.put("point1", new MyCoord(10, 20));
multiplePoints.put("point2", new MyCoord(100, 2000));
MyCoord coord=multiplePoints.get("point1");
System.out.println(coord.getX() +" : "+coord.getY());
}
}