Posts

Showing posts with the label object

Add null-like value to String in Java

Image
Clash Royale CLAN TAG #URR8PPP Add null-like value to String in Java I am writing test method like setTask(Task task) . And Task object has several fields, e.g. setTask(Task task) Task public String vehicle; Method setTask should be used in different test-cases, so I'd like to have an options for this field to accept values: setTask null some string value "", "Hello, World!", "Iso Isetta", ... random So what can I do to make String to be SpecialString which could accept values null , random & some string value ? (BTW: I don't want to set it to string value "RANDOM", and chech whether the value is equal to "RANDOM"-string) String SpecialString null random some string value UPDATE: I don't mean random like random value from a set of values , I mean random as well as null and this is for setTask() to handle random (select random from drop-down), and not to pass a random string from a set of values. random random ...

I'm trying to work with Graphics and draw some Rectangles

Image
Clash Royale CLAN TAG #URR8PPP I'm trying to work with Graphics and draw some Rectangles Hello i'm trying to learn Graphics in java and at the same time Classes and Objects. My goal now is to make a programm that contains different classes with Rectangles or Circles and then I want to use those Rectangles and Circles in other classes and change their parameters like size, color and position to draw some kind of Pattern. My problem right now is that I can make a rectangle and I think I can even make a second one, but i can't change parameters of it (Color, size and position) I tried adding variables to this part of code Rect rect = new Rect(int variables); but it didn't work. Rect rect = new Rect(int variables); Normally I can solve easy problems like this but i really don't understand how classes and objects works in java if someone can give me some help would be great. Here is my code public class Main{ public static void main(String args ) { Pattern.d...

Loop through List in Android Studio

Image
Clash Royale CLAN TAG #URR8PPP Loop through List<Object> in Android Studio I have a class called verse: public class Verse { String book; int chapter; int verse; String contents; } Which is populated from a database into: List<Object> list = new ArrayList<>(); List<Object> list = new ArrayList<>(); My question is how do I loop through the list and get each book, chapter and verse and contents? I've tried: for(int x=0; x < list.size(); x++) { int book = list.get(x); } But I'm unsure what .get(x) is returning, and I have a feeling it might the entire row, but I dont know how to get the data out of it. .get(x) Can anyone help? I'm very new to android studio. "I have a feeling it might the entire row". Well, if you're unsure what get(x) is returning it's unlikely any of us will know. so let's start here, why don't you print list.get(x) to the console and...

Initializing objects in Java without declaring a class or using a class

Image
Clash Royale CLAN TAG #URR8PPP Initializing objects in Java without declaring a class or using a class I have general question regarding objects in Java. I want to initialize an object in Java without declaring a class or using a class. In python for example you can do like this: myObject = {"x":5,"y":10,"z":12,"name":"something"} Now myObject is an object. And I can say myObject.x which will be 5. From what I know I would have to do like this in Java: class object(){ int x; int y; int z; String name; myObject(int x, int y, int z, String name){ this.x = x; this.y = y; this.z = z; this.name = name; } } myObject = new object(10,5,12,"something") I would like to do that, but like in my python example. Hope anyone can help :) "And I can say myObject.x which will be 5." No you can't. You're thinking of JavaScript. (Though I guess in JS you...

how to add an item to the method of an object in javascript

Image
Clash Royale CLAN TAG #URR8PPP how to add an item to the method of an object in javascript I want to add bar into the items array. bar items let myObj = { item:_ => ['foo'] } Tried doing this: myObj.item().push('bar') but when i do console.log(myObj.item()) i get back ['foo'] Any reasons for this behaviour ? console.log(myObj.item()) ['foo'] Yeah, it is how it works. Your function returns ['foo'] , each time you call it. Because you implemented it this way. – sjahan 6 mins ago ['foo'] if you want an array then just write let myObj = {item:['foo']} , no need for a method to add an array as property. – mpm 3 mins ago ...

Object instantiation variants in scala

Image
Clash Royale CLAN TAG #URR8PPP Object instantiation variants in scala I am really new to scala, and I am currently making my way through the tour (https://docs.scala-lang.org/tour/variances.html). Now, looking at some library ( akka-http ), I stumbled across some code like this: akka-http def fetchItem(itemId: Long): Future[Option[Item]] = Future { orders.find(o => o.id == itemId) } And I don't quite understand the syntax, or more precisely, the = Future { part. As I learned, the syntax for methods is def [methodName]([Arguments])(:[returnType]) = [codeblock] . = Future { def [methodName]([Arguments])(:[returnType]) = [codeblock] However the above seems to differ in that its having the Future in front of the "codeblock". Is this some kind of object instantiation? Because I could not find documentation about this syntax, I tried in my play code stuff like this: Future { val myCat:Cat = new Cat("first cat") val myOtherCat:Cat = Cat { "seco...

Analytic geometry using Python OOP

Image
Clash Royale CLAN TAG #URR8PPP Analytic geometry using Python OOP So I've just delved into OOP programming for the first time. I came across an exercise where we're supposed to write a script using OOP that creates a rectangle, calculates it's area and circumference, and the area of overlapping with a second rectangle. So far, so good, I managed to work it out. However, once I had solved it, I checked the given solution, and it looks something like this: from copy import copy class Point: def __init__(self, x=0.0, y=0.0): self.x = x self.y = y def __repr__(self): return '({}, {})'.format(self.x, self.y) class Rectangle: def __init__(self, point, width, height): self.point = copy(point) self.width = abs(width) self.height = abs(height) if self.width == 0: self.width == 1 if self.height == 0: self.height == 1 def __repr__(self): return '[{}, w = {}, h = ...

How to take the string from input of argparse and use it as an object

Image
Clash Royale CLAN TAG #URR8PPP How to take the string from input of argparse and use it as an object I am trying to create a program that reads a string off an argparse input and uses that string to call a certain object and use that object for the rest of the function. #!/usr/bin/python import argparse class car: def __init__(self, color, year): self.color = color self.year = year Beatle = car("blue", 1973) parser = argparse.ArgumentParser() parser.add_argument("--vehicleType") args = parser.parse_args() print("This "+args.vehicleType+ " is nice") print("It was made:") print(2018-args.vehicleType.year) print("years ago") However, I keep getting back this error: Traceback (most recent call last): File "./test.py", line 17, in <module> print(2018-args.vehicleType.year) AttributeError: 'str' object has no attribute 'year' I think the program is reading args.vehicleTyp...